Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let app run in background in android

Dialog notify the user to disable battery optimize for one app

Hi: I'm curiously about how to show this dialog. When I press allow, the battery optimize is disabled for this app. Is it an new android api

like image 944
hy fa Avatar asked Feb 19 '18 04:02

hy fa


People also ask

What does letting apps run in the background do?

Letting apps run in the background means that they can stay up to date – with news headlines or weather reports, for instance – and provide you with useful notifications. How do you control it? Most apps are set to run in the background by default.

How do you keep an app running in the background Samsung?

On Android 10 and above, you could simply swipe up from the bottom of your screen and hold it to access the recents screen. Now, locate the app you want to lock and press and hold its app icon at the top. When you get the pop-up, tap on 'Lock this app. '


2 Answers

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String packageName = context.getPackageName();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                Intent intent = new Intent();
                intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.parse("package:" + packageName));
                context.startActivity(intent);
            }
        }

Also, you need to add the following permission in manifest

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
like image 90
hy fa Avatar answered Oct 01 '22 07:10

hy fa


This is part of the new App Standby feature introduced with API 23 (Marshmallow) alongside Doze Battery Optimization aimed to optimize power and resource usage while the app is in background (App Standby) or when the device has long been in sleep (Doze).

Following is the explanation from the Android Developer's site page:

Specifically, in the App Standby mode, the system determines that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time and none of the following conditions applies:

  • The user explicitly launches the app.
  • The app has a process currently in the foreground (either as an activity or foreground service, or in use by another activity or foreground service).
  • The app generates a notification that users see on the lock screen or in the notification tray.
  • The app is an active device admin app (for example, a device policy controller). Although they generally run in the background, device admin apps never enter App Standby because they must remain available to receive policy from a server at any time.

When the user plugs the device into a power supply, the system releases apps from the standby state, allowing them to freely access the network and to execute any pending jobs and syncs. If the device is idle for long periods of time, the system allows idle apps network access around once a day.

So, this means that starting from API 23 (Marshmallow), the device may actively put your app on standby, preventing network access (say for task like sync) and limiting (or disabling) background executions. Now, for most of the apps this behavior is fine or you could easily optimize for such behavior, but for some apps out there this may cause some unexpected behavior, especially for apps that have been poorly optimized or use non-standard sync strategies or some other means of background sync/execution. So to circumvent that, the user can explicitly mark the app as non-optimized and the system will fallback and not put the app to standby, although this leads to a poorer user experience and you should not be doing this for regular apps that could be optimized.

like image 21
riyaz-ali Avatar answered Oct 01 '22 08:10

riyaz-ali