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

前端 未结 6 1524
無奈伤痛
無奈伤痛 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:47

    If you start a service in Application Class than your service will be always running even though if a user terminates or force stop from task manager, it will run again.

    To create service specifically in Android studio Right click on app from Project Explorer and then New > Service > Service

    Create a service:

    public class ServiceName extends Service {
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
        return null;
      }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
          // do your jobs here
          return super.onStartCommand(intent, flags, startId);
            }
        } 
    

    Now create an Application Class and start Service in Application Class

    public class App extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            startService(new Intent(this, ServiceName.class));
        }
    }  
    

提交回复
热议问题