简体   繁体   English

启动完成时启动服务无法正常工作

[英]Start service on boot completed not working

My broadcastreceiver doesnt work, i dont get the message in the log, can you please help me? 我的广播接收器不起作用,我没有在日志中收到消息,请您能帮我吗? This is my broadcastreceiver: 这是我的广播接收器:

public class BootReceiver extends BroadcastReceiver{
    public static SharedPreferences prefs;

    @Override
    public void onReceive(Context context, Intent intent) {
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        // TODO Auto-generated method stub
        Log.w("A intrat in BootReceiver"," ");
        if (!(prefs.getString(NotificareOptions.OptionsPos, "2")).equals("1"))
        context.startService(new Intent(context, ServiceNotif.class));

    }

}

i got the permission, and i have the receiver declared in the manifest. 我获得了许可,并且清单中声明了接收方。

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

 <receiver android:name=".BootReceiver"  
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="500" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Where is the problem? 问题出在哪儿? I also heard that RECEIVE_BOOT_COMPLETED doesnt work on all android phones. 我还听说RECEIVE_BOOT_COMPLETED用于所有Android手机。

Your code looks correct. 您的代码看起来正确。 This may be an issue with the emulator. 这可能是模拟器的问题。 I too had problems with the BOOT_COMPLETE broadcast on the emulator. 我在模拟器上广播的BOOT_COMPLETE也有问题。 It did not always fire when re-starting the emulator. 重新启动模拟器时,它并不总是会触发。 The following ADB command did the trick however: 但是,以下ADB命令可以解决问题:

adb.exe -s emulator-5554 shell
# am broadcast -a android.intent.action.BOOT_COMPLETED

This will cause the emulator to broadcast a BOOT_COMPLETED message, just as a real device does at startup. 就像真实设备在启动时一样,这将导致仿真器广播BOOT_COMPLETED消息。 This also has the advantage of being much faster than re-starting the emulator. 这也具有比重新启动仿真器快得多的优点。

After i replaced android:exported="true" with android:exported="false" in my manifest receiver it started working. 在清单接收器中将android:exported="true"替换为android:exported="false" ,它开始工作。 In many tutorials it doesn't say about it, but maybe this could help someone else too. 在许多教程中都没有对此进行说明,但是也许这也可以对其他人有所帮助。

A couple of things I notice. 我注意到了几件事。 In your BootReceiver you specify: 在您的BootReceiver中,指定:

<receiver android:name=".BootReceiver"

This implies that the receiver is in the default package (eg in your manifest the value of package ). 这意味着接收者位于默认包中(例如,清单中的值为package )。 If this is not the case, or if you are unsure, you can specify the full package like this: 如果不是这种情况,或者不确定,可以这样指定完整的软件包:

<receiver android:name="com.mycompany.boot.BootReceiver"

Also, it's possible that your log message is working but you cannot see it. 另外,您的日志消息可能正在工作,但看不到它。 You log as: 您登录为:

Log.w("A intrat in BootReceiver"," ");

So the tag is "A intrat in BootReceiver" and the message is empty. 因此,标记为“ BootReceiver中的Intrat”,消息为空。 Try putting in a message value that isn't empty. 尝试输入不为空的消息值。

Also, you can see that if Intent was fired or not if you have adb logcat running when the device reboots. 此外,您还可以看到,如果在设备重新引导时运行了adb logcat ,则是否触发了Intent。

I can't see anything directly wrong. 我看不到任何直接错误。 But a couple of things to try: 但是有几件事可以尝试:

  1. Try naming your receiver with its full package.name.path.BootReceiver 尝试使用完整的package.name.path.BootReceiver命名接收器
  2. Remove enabled, exported and priority from the receiver declaration 从接收者声明中删除已启用,已导出和优先级

Based on your indication that changing the value of android:exported resolved your issue, I think something else might have been in play -- rebuilding your code from scratch. 根据您的指示,更改android:exported的值可以解决您的问题,我认为可能还有其他问题–从头开始重建代码。

Conducting a 'Clean' in Eclipse (or the equivalent command in your development environment) could resolve this problem for others in the future. 在Eclipse中执行“清理”(或开发环境中的等效命令)可能会在将来为其他人解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM