Android alarms cancelled when application is kille

2019-05-04 05:42发布

问题:

I have an Android application that has a service that runs in the background, communicates with a remote server and displays notifications to users if needed. The app also has an activity for users to interact with.

One of the requirements is that the application needs to start itself automatically in the morning. At the end of the day the user is free to quit the app (Quit functionality stops the service, releases the wake lock, and lets Android kill the process). In order to automatically restart the application I am scheduling an alarm using AlarmManager and PendingIntent:

    //create intent to alert OnStartReceiver when alarm goes off
    Intent i = new Intent(context, OnStartReceiver.class);
    i.setAction("com.pointtrace.intents.alarm");
    i.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION|Intent.FLAG_FROM_BACKGROUND);


    //wrap inside PendingIntent so that it can be used from outside
    //our application
    PendingIntent pi = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarms = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Log.d("GXT", "Scheduling alarm action(i)=" + i.getAction());

    //set alarm to go off at wake time
    alarms.set(AlarmManager.RTC_WAKEUP, getWakeTime(), pi);

OnStartReceiver class is registered to receive broadcasts and will restart the app when alarm occurs.

Everything works fine unless the user kills the application from Task Manager. If application is exited normally by either being killed by Android OS or my me calling System.exit(0); it restarts fine. But when it is killed from Task Manager is looks almost like alarm is getting cancelled.

I could not find anything in the Android documentation that would imply that alarms would be cancelled by Task Manager. Am I doing something wrong in regards to how to properly set the alarms?

I've tried removing the flags and just using 0 but it did not make any difference.

Thanks.

回答1:

Quit functionality stops the service, releases the wake lock, and lets Android kill the process

If you are implying that you have a continuous WakeLock, unless this is only running on devices that are perpetually plugged in, your users will slap you with a trout.

Also, Android can kill the process whenever it wants. Do not mistake startForeground() as some sort of guarantee that Android will never kill your process.

But when it is killed from Task Manager is looks almost like alarm is getting cancelled.

I have no idea what "Task Manager" is, since there is none in Android. If you mean the list of running apps in Settings, and clicking Force Stop, then yes, that cancels any outstanding alarms and prevents your app from ever running again until something manually starts one of your components (e.g., user launches your app from the launcher).

Am I doing something wrong in regards to how to properly set the alarms?

Probably not. Force-stop definitely wipes out your alarms.



回答2:

I lost 1 month of my time for the same issue. Finally I found the solution. From Android versions > 21, an option called 'Auto Launch' have been introduced, which means the user can actually configure whether any app can be automatically started or not. So please check whether your app has permission for auto launch. This setting location may vay based on phone manufacturer. So you need to search for this 'Auto Lanuch' option in your phone settings. Even if it doesn't work, try removing your app from the 'Optimizations List' as well, which you can find in the settings.