我一直试图在设备在android上启动时启动一项服务,但我无法让它工作。我在网上看了一些链接,但没有一个代码工作。我是不是忘了什么?

AndroidManifest.xml

<receiver
    android:name=".StartServiceAtBootReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action._BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.test.RunService"
    android:enabled="true" />

BroadcastReceiver

public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceLauncher = new Intent(context, RunService.class);
        context.startService(serviceLauncher);
        Log.v("TEST", "Service loaded at start");
    }
}

当前回答

参考此链接http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.html 在服务上使用引导的步骤

其他回答

随着

<action android:name="android.intent.action.BOOT_COMPLETED" />  

也使用,

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

HTC设备似乎没有捕捉BOOT_COMPLETED

作为附加信息:BOOT_COMPLETE在外部存储挂载之前发送给应用程序。因此,如果应用程序被安装到外部存储,它将不会收到BOOT_COMPLETE广播消息。

更多详细信息,请参见广播接收器侦听“引导完成”部分。

在尝试了所有提到的答案和技巧后,我终于找到了为什么代码不能在我的手机上工作。一些Android手机,如“华为荣耀3C Android 4.2.2”,在他们的设置中有一个状态管理器菜单,你的应用程序必须在列表中勾选。 :)

如果在接收类中保留空构造函数,就会遇到这个问题。删除空构造函数后,onRreceive方法开始正常工作。

如何在设备启动时启动服务(autorun app等)

首先:自版本Android 3.1+你不会收到BOOT_COMPLETE如果用户从未启动你的应用程序至少一次或用户“强制关闭”应用程序。 这样做是为了防止恶意软件自动注册服务。这个安全漏洞在最新版本的Android中已经被修补。

解决方案:

创建带有活动的应用程序。当用户运行它一次应用程序可以收到BOOT_COMPLETE广播消息。

第二:在挂载外部存储之前发送BOOT_COMPLETE。如果app被安装到外部存储,它不会收到BOOT_COMPLETE广播消息。

在这种情况下,有两个解决方案:

将应用程序安装到内部存储 在内部存储中安装另一个小应用程序。这个应用程序接收BOOT_COMPLETE并在外部存储上运行第二个应用程序。

如果你的应用程序已经安装在内部存储中,那么下面的代码可以帮助你理解如何在设备启动时启动服务。


在Manifest.xml

许可:

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

注册你的BOOT_COMPLETED接收器:

<receiver android:name="org.yourapp.OnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

注册您的服务:

<service android:name="org.yourapp.YourCoolService" />

在接收器OnBoot.java中:

public class OnBoot extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // Create Intent
        Intent serviceIntent = new Intent(context, YourCoolService.class);
        // Start service
        context.startService(serviceIntent);

    }

 }

对于HTC,如果设备没有捕获RECEIVE_BOOT_COMPLETED,你可能还需要在Manifest中添加这段代码:

<action android:name="android.intent.action.QUICKBOOT_POWERON" />

接收器现在看起来是这样的:

<receiver android:name="org.yourapp.OnBoot">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

如何测试BOOT_COMPLETED没有重新启动模拟器或真实设备? 很容易。试试这个:

adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED

如何获取设备id?获取带有id的连接设备列表:

adb devices

默认情况下,您可以在ADT中找到:

adt-installation-dir/sdk/platform-tools

享受!)