How to exclude an Android App from Battery Optimization using code

Viewed 6088

I am new to Android, now I am working on a Project which is based on GPS. I got source code from internet(traccar). my requirement is like the app should update location on each 1Km or each 1hr. but the problem is the app not working in background after some time(10 - 20 mins). Is there any solution for this ?

what should I do(in code) to exclude this app from battery optimisation when the app is launching ? is it possible ?

4 Answers

I think you're having 2 different problems:

1) If you want to keep your app in background you should use a foreground Service. That way your app won't be considered to be in background by the system and the chances of its process being killed are reduced drastically. The downside is that as long as your Service is in foreground you need to show a permanent notification.

2) You cannot exclude your app from battery optimization yourself, but you can prompt the user the settings to whitelist your app. In order to do that you can refer to the official docs, you'll need to add the Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission to the manifest and then launch an intent with action ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS. The user will then be able to whitelist your app, only she/he can do that because otherwise every app would whitelist itself and the purpose of the battery optimization would be defied.

Add this permission in your manifest file Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

Request Permission at runtime inside onCreate method of your activity...

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);
            }
        }

this is the image of the code in debug mode

this will be the view in app

but the application not working as it is in No Restriction mode (background activity). I want the application to work as No Restriction mode.

Add this permission in your manifest file Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

Request Permission at runtime inside onCreate method of your activity...

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
}

Here is the kotlin version of @Shafeeq Mohammed Answer and it worked for me. Thank you

         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)
                }
            }
        }
Related