如何在启动时启动我的应用程序?

我试着使用this链接中的示例代码,但它似乎已经过时了,而且它没有工作。那么,我需要对哪些文件进行修改,才能使我的应用程序在安卓完成启动时自动启动?

首先,你需要在你的AndroidManifest.xml中设置权限。

同时,在你的AndroidManifest.xml中,定义你的服务并监听BOOT_COMPLETED动作。











然后,你需要定义接收器,以获得BOOT_COMPLETED动作并启动你的服务。

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startService(serviceIntent);
        }
    }
}

现在你的服务应该在手机启动时运行。

评论(11)

听听ACTION_BOOT_COMPLETE,然后做你需要做的事。 这里有一个代码片段

更新:

答案上的原始链接已经失效,所以根据评论,这里是链接的代码,因为当链接失效时,没有人会错过这个代码。

在AndroidManifest.xml(应用程序部分)。







...

...

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);  
        }

}

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

评论(7)

此外,如果你不想修改代码,你可以使用像AutoStart这样的应用程序,在启动时启动一个安卓应用程序。自动启动--无根

评论(1)