How Google calendar or Instagram make their apps all the time up and running even after force stop

前端 未结 6 1523
無奈伤痛
無奈伤痛 2021-02-05 09:24

Looks like force stop should prevent app from running and it\'s even disable all app\'s alarms. However I found that notification in Google Calendar still shown fine even after

6条回答
  •  醉酒成梦
    2021-02-05 09:52

    Displaying notifications is a common functionality of Android Services and there are already many questions on how to prevent Services from being stopped/or from force close.

    The relevant questions are listed below:

    Android: How to auto-restart application after it's been "force closed"?

    How to prevent android service getting killed (Service with notification)

    How to restart a service in android?

    Keep Service running

    Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?

    To summarise them, the first step is to implement a BroadcastReceiver that listens for BOOT_COMPLETED. This will mean that your service becomes active when the Android device is turned on. To do this, you put the following code in the manifest:

    
        
            
        
    
    

    Make sure also to include the completed boot permission:

    
    

    Then implement the BroadcastReceiver:

    public class MyBootCompletedReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent myService = new Intent(context, MyLongRunningService.class);
            context.startService(myService);
        }
    }
    

    When you start the Service, you can return START_STICKY which will tell the Android OS to restart the service if it is squashed by the OS in conditions of limited resources:

    public class MyLongRunningService extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
            //inject dependencies if necessary
        }
    
        public int onStartCommand(Intent intent, int flags, int startId) {
            //setup, then
            return START_STICKY;
        }
    }
    

    In general, Services launched by apps will be stopped by the Android OS when the device is low on resources. Returning START_STICKY tells the OS to restart the Service if it has to be stopped. Hence, by ensuring the Service is started when the device is turned on and by requesting it to be restarted if it is stopped under conditions of low resource, you have made a Service that runs constantly.

    This is the most basic pattern for ensuring your Service (and hence Application) is running all the time, which was your requirement.

    There are additional measures on top of that which you can take which are outlined in the other answers here, but they should be seen as a supplement to implementing a Service that starts on BOOT_COMPLETED and that requests restart if it is stopped in conditions of low resources.

提交回复
热议问题