如何开始启动一个应用程序? [英] How to start an Application on startup?

查看:171
本文介绍了如何开始启动一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个样本code,但似乎类和它们使用的字符串常量已经过时,不再提供。另外,你能告诉我什么样的变化,以使的Andr​​oidManifest.xml 文件。我发现一个例子code。在下面的链接

I found a sample code, but it seems that the classes and string constants used in them are outdated and are no longer provided. Also can you tell me what changes to make in the AndroidManifest.xml file. I found an example code at the following link

推荐答案

首先,你需要在允许你的的Andr​​oidManifest.xml

First, you need the permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

此外,在你的的Andr​​oidManifest.xml ,定义服务,并听取了 BOOT_COMPLETED 操作:

Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

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

Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.

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

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

And now your service should be running when the phone starts up.

这篇关于如何开始启动一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆