How to exclude an Android App from Battery Optimization using code

android disable battery optimization programmatically
android battery optimization code
battery optimization flutter
android exclude app from power saving
disable doze mode android programmatically
how to stop apps from draining battery android programmatically
request_ignore_battery_optimizations
battery optimization vivo

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 ?

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.

Set specific App to ignore optimization by code in Android M, WARNING: It seems Google doesn't allow apps on the Play Store to do this. See the comments below. Based on the AndroidManifest.xml  Tap Battery Optimization. Locate and tap the app you want to add back to the list. Tap Optimize. Tap DONE. Your app is back on the list and will be placed on standby.

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.

How to remove Android apps from the battery optimization list , Android Marshmallow came with a whole host of new features, including battery optimization via the app standby mode. This feature puts apps  Android's battery optimization - Doze A Doze mode was introduced with Android 6.0 Marshmallow and improved upon subsequently in Android 7.0 Nougat and Android 8.0 Oreo. What it essentially does is hibernate apps when you're not using them. Limiting the background activity for apps is a good way to squeeze out some extra juice off your battery.

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
}

How to turn off battery optimization on the Google Android 6, 7 and 8 , How to turn off battery optimization on the Google Android 6, 7 and 8 devices To turn off optimization function in battery optimization, please refer to the to the All apps list from the drop-down to see all of the apps on your device. Tap Nine from the menu and choose Don't optimize to exclude Nine from the Doze feature. To change Android Marshmallow battery optimization, you need go to Settings — Battery first. In the battery usage page as shown below, tap the menu button (3 vertical dots). In Samsung devices, MORE button is usually used as menu button. In battery menu, tap battery optimization as shown below.

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

Android, Open phone's settings; open battery; open menu and select "battery optimization"​; from the dropdown menu select "all apps"; select the app you want to whitelist  This answer is based off my Nexus 9 on MRA58K. It should be similar for any app. Follow the steps as given below: Go to Settings → Battery. Tap on the top right 3-dot menu, and choose Battery Optimisation. There should be a dropdown below the actionbar on the top left, choose All Apps from the list. Find your app in the list (in this case

Turn Battery Optimization On / Off, Battery optimization helps conserve battery power on your LG device and is turned on by default. Apps with optimization turned off may continue to impact battery life. From a Home Tap 'Ignore optimizations' or 'Power saving exclusions'. The code below works fine on a lot (tenth) of devices. It shows the settings panel to prompt the user to exclude the app from the Android battery optimization. If GetAndroidApiLevel>=23 Then Dim JavaObject1 As JavaObject JavaObject1.InitializeContext Dim Ignoring As Boolean=JavaObject1.RunMethod ("isIgnoringBatteryOptimizations", Null) If Ignoring=False Then Msgbox ("Please confirm the following form to exclude the app from battery optimization.", "Battery optimization") 'RuntimePermissions1.

Turn Battery Optimization On / Off - Android 6.x and higher, Battery optimization features improve battery life by placing apps in Doze mode or App Standby. Optimization is turned on by default and can be turned off / back​  Turn Battery Optimization On / Off - Android 6.x and higher (LG) Turn Battery Optimization On / Off - Android™ 6.x and higher (LG) Connect with us on Messenger .

How To Exclude Certain Apps From Android's Battery-Saving Doze , How To Exclude Certain Apps From Android's Battery-Saving Doze Feature You may have heard one of the new features in Android Marshmallow is Doze, a standby mode for your apps that Error Code: PLAYER_ERR_GEO_RESTRICTED can select any installed app to exclude it from battery optimization (Doze). If the Battery screen shows that an app is using a lot of battery power, you may want to remove or replace it. Apps definitely appear on your Battery screen if you actually use them, but some apps are heavier power users than others—for example, a demanding 3D game uses more battery power than the average app.

Comments
  • If you want to get location in background then you need to use Service with get accurate location using FusedLocationProviderAPI. If your GPS and service will be continued then battery will be used. But for your optimization you can get location using High or Low accuracy.
  • REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is not a dangerous permission according to the docs, you don't ask runtime permission for that. Also it just allows your app to be whitelisted, the permission itself doesn't whitelist your app.
  • let me try this
  • since I am new to android now I am confused. I don't know where I have to use this code
  • use inside onCreate method of your launcher activity
  • @SachinSoma how is that going to fix the problem? The official docs seem pretty clear