简体   繁体   English

BroadcastReceiver无法正常工作

[英]BroadcastReceiver is not working

I have implemented this broadcast reciever: 我已经实现了这个广播接收器:

public class ServiceManager extends BroadcastReceiver {
    private final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";
    private final String BOOT_ACTION_FIRST_LAUNCH = "android.intent.action.PACKAGE_FIRST_LAUNCH";
    private final String BOOT_ACTION_RESTARTED = "android.intent.action.PACKAGE_RESTARTED";

    @Override
    public void onReceive(Context context, Intent intent) {
        // All registered broadcasts are received by this
        String action = intent.getAction();
        if (action.equalsIgnoreCase(BOOT_ACTION) || action.equalsIgnoreCase(BOOT_ACTION_FIRST_LAUNCH) || 
                action.equalsIgnoreCase(BOOT_ACTION_RESTARTED)) {
             // TODO: Action
        } 
    }

}

AndroidManifest.xml AndroidManifest.xml中

<receiver android:name="package.service.ServiceManager" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
        <action android:name="android.intent.action.PACKAGE_RESTARTED" />
    </intent-filter>
</receiver>

The BOOT_COMPLETED action is working right, but, the PACKAGE_FIRST_LAUNCH and PACKAGE_RESTARTED are not working. BOOT_COMPLETED操作正常,但是,PACKAGE_FIRST_LAUNCH和PACKAGE_RESTARTED无法正常工作。 I need to launch my broadcast receiver when I launch my app, that's why I'm using these actions. 我需要在启动应用程序时启动广播接收器,这就是我使用这些操作的原因。 But, when I launch or restart the app, the receiver is not working. 但是,当我启动或重新启动应用程序时,接收器无法正常工作。 It only works when I restart my mobile phone. 它只在我重新启动手机时才有效。 Are there something wrong in my source? 我的来源有什么问题吗?

FYI: PACKAGE_FIRST_LAUNCH is only sent to the installer package, ie whatever you used to install the application - for most end users that would be Android Market. 仅供参考: PACKAGE_FIRST_LAUNCH 发送到安装程序包,即用于安装应用程序的任何内容 - 适用于大多数最终用户,即Android Market。

Edit: 编辑:
Oh, and for "PACKAGE_RESTARTED", break that one out into its own <intent-filter> and add a 哦,对于“PACKAGE_RESTARTED”,将其中的一个打破到自己的<intent-filter>并添加一个

<data android:scheme="package"/>

since that one comes with an URI and an explicit scheme. 因为那个带有URI和显式方案。

Logically it seems that PACKAGE_FIRST_LAUNCH will be broadcasted once your app is run for the first time after boot/reboot. 从逻辑PACKAGE_FIRST_LAUNCH一旦你的应用程序在启动/重启后第一次运行, PACKAGE_FIRST_LAUNCH就会被广播。 And PACKAGE_RESTARTED should be broadcasted if your application activity stack is removed and then your app is clicked to start again (like restart). 如果删除了应用程序活动堆栈,然后单击您的应用程序再次启动(如重新启动),则应广播PACKAGE_RESTARTED

However, you may simply achieve this by broadcasting a custom action string when ever your app is launched (perhaps from your first activity). 但是,您可以通过在启动应用程序时(可能来自您的第一个活动)广播自定义操作字符串来实现此目的。

The intent android.intent.action.PACKAGE_FIRST_LAUNCH is introduced in Android API Level 12. If you are using lesser API Level it will not work. 意图android.intent.action.PACKAGE_FIRST_LAUNCH是在Android API Level 12中引入的。如果您使用较小的API级别,它将无法工作。 So change your project settings accordingly. 因此,相应地更改项目设置。

Manifest: 表现:

...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></>
...
<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
...

Receiver: 接收器:

package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoStart extends BroadcastReceiver
{   
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            // Your code
        }
    }
}

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

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