How do I start my app on startup?

前端 未结 8 1776
失恋的感觉
失恋的感觉 2020-11-22 01:06

I tried using the sample code in this link but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically

8条回答
  •  孤独总比滥情好
    2020-11-22 01:46

    Listen for the ACTION_BOOT_COMPLETE and do what you need to from there. There is a code snippet here.

    Update:

    Original link on answer is down, so based on the comments, here it is linked code, because no one would ever miss the code when the links are down.

    In AndroidManifest.xml (application-part):

    
    
            
                    
                    
            
    
    

    ...

    
    

    ...

    public class BootUpReceiver extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
                    Intent i = new Intent(context, MyActivity.class);  //MyActivity can be anything which you want to start on bootup...
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);  
            }
    
    }
    

    Source: https://web.archive.org/web/20150520124552/http://www.androidsnippets.com/autostart-an-application-at-bootup

提交回复
热议问题