BroadcastReceiver is not working

冷暖自知 提交于 2019-12-07 13:54:47

问题


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

<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. 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?


回答1:


Logically it seems that PACKAGE_FIRST_LAUNCH will be broadcasted once your app is run for the first time after boot/reboot. And PACKAGE_RESTARTED should be broadcasted if your application activity stack is removed and then your app is clicked to start again (like restart).

However, you may simply achieve this by broadcasting a custom action string when ever your app is launched (perhaps from your first activity).




回答2:


FYI: PACKAGE_FIRST_LAUNCH is only sent to the installer package, i.e. whatever you used to install the application - for most end users that would be Android Market.

Edit:
Oh, and for "PACKAGE_RESTARTED", break that one out into its own <intent-filter> and add a

<data android:scheme="package"/>

since that one comes with an URI and an explicit scheme.




回答3:


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



回答4:


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. So change your project settings accordingly.



来源:https://stackoverflow.com/questions/9683201/broadcastreceiver-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!