Running an application in the background when the phone is in Doze

The application runs SDK 23 and above. I perform some tasks using the Service after completing the task and scheduling the next task using AlaramManager (setExactAndAllowWhileIdle). This is working fine. If the phone does not work for 2 or 3 days, it goes into the "Dosage" mode. After Doze mode, the application losing network and wakelock also does not work.

Is there a way, even if the phone is Doze, we can launch the application with any problems related to network interferences. I tried to save the witelist application, but I need the device to be deployed.

adb shell dumpsys deviceidle whitelist +<Package Name>

Can someone suggest me what is the best way to run the application without interruptions?

+6
source share
4 answers

First, instead of calling the service directly, AlarmManagercall the broadcast receiver, which then calls the service.

The broadcast receiver should distribute WakefulBroadcastReceiverinstead of the usual one BroadcastReceiver.

And then, let the broadcast receiver schedule a new Alarm, start the service using startWakefulService()insteadstartService()

public class MyAwesomeReceiver extends WakefulBroadcastReceiver {
int interval=2*60*60*1000;
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent serviceIntent = new Intent(context, MyAwesomeService.class);
        Intent receiverIntent = new Intent(context, MyAwesomeReceiver.class);

        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 11, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+interval,alarmIntent);
        startWakefulService(context, serviceIntent);
        }
}

WakefulBroadcastReceiverand startWakefulService()let your application open a 10 second window to do what it needs.

Moreover,

You can always ask the user to let your application ignore the battery optimization functionality with -

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (powerManager.isIgnoringBatteryOptimizations(getPackageName())) {
     intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
}
else {
     intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
     intent.setData(Uri.parse("package:" + getPackageName()));
     startActivity(intent);
}

and in the manifest

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"></uses-permission>
+2
source

, GCM . , Google Play:

Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases

+1

, . , , , - .

. Android, , , . , . , , , . , .

, . , .

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if(intent.getAction().equals("android.os.action.DEVICE_IDLE_MODE_CHANGED")){
        if (pm.isDeviceIdleMode()) {
            startForegroundService();
            //stopAlarmManagerLogic();
        } else {
            stopAutomationForegroundService();
            //startAlarmManagerLogic();
            return;
        }
        AutomationReceiver.completeWakefulIntent(intent);
        return;
    }
}
+1

After Android N, the app cannot run in the background forever. However, you can use the Firebase job manager, which can help you launch the application, even if it is in dose mode. Using Firebase Task Manager, you can tell the system that your application should run at a specific time, provided that the conditions are agreed.

0
source

Source: https://habr.com/ru/post/1016645/


All Articles