BroadcastReceiver not working

I have implemented this broadcast receiver:

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 works correctly, but PACKAGE_FIRST_LAUNCH and PACKAGE_RESTARTED don't work. I need to start my broadcast receiver when the application starts, so I use these steps. But when I start or restart the application, the receiver doesn't work. It only works when I restart my cell phone. Is there something wrong with my source?

+3


source to share


4 answers


Logically, it seems like it PACKAGE_FIRST_LAUNCH

will be broadcast after launching your application for the first time after loading / reloading. And PACKAGE_RESTARTED

should be broadcast if your application activity stack is removed and then your application will be pushed to start over (e.g. restart).



However, you can simply achieve this by passing in a custom action string when your application starts (perhaps from your first action).

+3


source


FYI: PACKAGE_FIRST_LAUNCH

sent only to the Installer package, that is all that you used to install the application, for the majority of end-users who will be Android Market.

Edit:
Oh, and for "PACKAGE_RESTARTED", break this into your own <intent-filter>

and add



<data android:scheme="package"/>

      

since it includes a URI and an explicit scheme.

+5


source


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

      

Recipient:

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

      

0


source


The target android.intent.action.PACKAGE_FIRST_LAUNCH

is presented in Android API Level 12. If you are using a lower API level, this will not work. So change your project settings accordingly.

0


source







All Articles