Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用代码从电池优化中排除Android应用程序_Java_Android_Android Studio - Fatal编程技术网

Java 如何使用代码从电池优化中排除Android应用程序

Java 如何使用代码从电池优化中排除Android应用程序,java,android,android-studio,Java,Android,Android Studio,我是Android新手,现在我正在做一个基于GPS的项目。我从互联网上获得了源代码(traccar)。我的要求是,应用程序应该每1公里或每1小时更新一次位置。但问题是应用程序在一段时间(10-20分钟)后不能在后台工作。有什么解决办法吗 当应用程序启动时,我应该如何(在代码中)将此应用程序从电池优化中排除?可能吗 我认为你有两个不同的问题: 1) 如果你想把你的应用程序放在后台,你应该使用前台服务。这样,你的应用程序就不会被系统视为处于后台,其进程被终止的几率也会大大降低。缺点是,只要您的服务在

我是Android新手,现在我正在做一个基于GPS的项目。我从互联网上获得了源代码(traccar)。我的要求是,应用程序应该每1公里或每1小时更新一次位置。但问题是应用程序在一段时间(10-20分钟)后不能在后台工作。有什么解决办法吗

当应用程序启动时,我应该如何(在代码中)将此应用程序从电池优化中排除?可能吗


我认为你有两个不同的问题:

1) 如果你想把你的应用程序放在后台,你应该使用前台服务。这样,你的应用程序就不会被系统视为处于后台,其进程被终止的几率也会大大降低。缺点是,只要您的服务在前台,您就需要显示一个永久通知


2) 您不能自己将应用程序从电池优化中排除,但您可以提示用户设置以将应用程序列入白名单。为了做到这一点,您可以参考,您需要将
Manifest.permission.REQUEST\u IGNORE\u BATTERY\u OPTIMIZATIONS
权限添加到清单中,然后启动一个intent with action。然后,用户将能够将您的应用程序列入白名单,只有她/他可以这样做,因为否则每个应用程序都会将自身列入白名单,并且电池优化的目的将受到挑战。

在清单文件中添加此权限
Manifest.permission.REQUEST\u忽略电池\u优化

在运行时在活动的onCreate方法内请求权限

private final int MY_PERMISSIONS_IGNORE_BATTERY_OPTIMIZATIONS =1;
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this,
            arrayOf(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS),
                MY_PERMISSIONS_IGNORE_BATTERY_OPTIMIZATIONS)
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
        String packageName = "org.traccar.client";
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent i = new Intent();
            if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
                i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                i.setData(Uri.parse("package:" + packageName));
                startActivity(i);
            }
        }

在清单文件manifest.permission.REQUEST\u IGNORE\u BATTERY\u优化中添加此权限

在运行时在活动的onCreate方法内请求权限

private final int MY_PERMISSIONS_IGNORE_BATTERY_OPTIMIZATIONS =1;
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this,
            arrayOf(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS),
                MY_PERMISSIONS_IGNORE_BATTERY_OPTIMIZATIONS)
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
        String packageName = "org.traccar.client";
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Intent i = new Intent();
            if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
                i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                i.setData(Uri.parse("package:" + packageName));
                startActivity(i);
            }
        }

但应用程序无法正常工作,因为它处于无限制模式(后台活动)。
我希望应用程序以无限制模式运行。

这是@Shafeeq Mohammed Answer的kotlin版本,它对我很有效。多谢各位

         fun checkBatteryOptimization(mContext: Context) {

            val powerManager =
                mContext.getSystemService(POWER_SERVICE) as PowerManager
            val packageName = mContext.packageName
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val i = Intent()
                if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
                    i.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
                    i.data = Uri.parse("package:$packageName")
                    mContext.startActivity(i)
                }
            }
        }

如果要在后台获取位置,则需要使用
服务
,并使用
FusedLocationProviderAPI
获取准确位置。如果您的GPS和服务将继续,则将使用电池。但是对于您的优化,您可以使用
精度获取位置。请求\u忽略\u电池\u优化不是危险的权限根据文档,您不需要请求运行时权限。另外,它只允许你的应用程序被列入白名单,权限本身并不会将你的应用程序列入白名单。让我试试这个,因为我是android新手,现在我很困惑。我不知道在你的启动器的onCreate方法中我必须在哪里使用这个代码activity@SachinSoma这将如何解决问题?官方文档看起来非常清晰,我尝试了您的解决方案,将上面的代码放在启动程序活动的onCreate方法中,但没有成功。只有当我将代码移到onStart时,它才起作用。可能与谁有关