Android - Start service on boot

前端 未结 7 1231
温柔的废话
温柔的废话 2020-11-22 16:05

From everything I\'ve seen on Stack Exchange and elsewhere, I have everything set up correctly to start an IntentService when Android OS boots. Unfortunately it is not start

7条回答
  •  旧时难觅i
    2020-11-22 16:17

    Following should work. I have verified. May be your problem is somewhere else.

    Receiver:

    public class MyReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
                Log.d("TAG", "MyReceiver");
                Intent serviceIntent = new Intent(context, Test1Service.class);
                context.startService(serviceIntent);
            }
        }
    }
    

    Service:

    public class Test1Service extends Service {
        /** Called when the activity is first created. */
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("TAG", "Service created.");
        }
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("TAG", "Service started.");
            return super.onStartCommand(intent, flags, startId);
        }
        
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.d("TAG", "Service started.");
        }
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    }
    

    Manifest:

    
    
        
    
        
        
        
        
        
    
            
            
              
                  
                     
                  
             
        
    
    

提交回复
热议问题