Start activity on boot

前端 未结 6 1167
梦毁少年i
梦毁少年i 2020-12-06 03:19

I\'d like to start my app just after the phone boot. Apparently the app is started after the boot but it immediately crashes (just to be clear the app normally works fine).

6条回答
  •  情深已故
    2020-12-06 03:53

    Try this:
    

    1] In AndroidManifest.xml file:

     
    
     
            
                
    
                
            
        
     
    

    2] Inside BroadcastReciever class with StartMyActivityAtBootReceiver as class name.

    @Override
    public void onReceive(Context context, Intent intent) {
    
        Intent i = new Intent(context, MainActivity.class);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i); 
    
    } 
    

    This worked for me. The difference in code is as follows:

    • android:permission="android.permission.RECEIVE_BOOT_COMPLETED" inside receiver.
    • included "category android:name="android.intent.category.DEFAULT" " inside intent filter.
    • I am not checking the intent in onRecieve, as i know that code will be executed only if its true

提交回复
热议问题