简体   繁体   中英

Dynamic register to broadcast receiver not working

When I'm setting my broadcast receiver as anonymous class his never called but when I'm setting it as class and declare it on android manifest its work fine

i want the ability of register and unregister the broadcast receiver dynamically why its won't working

here is my code:

public class AppChangedProbe extends Probe.Base implements Probe.ContinuousProbe{

private BroadcastReceiver appReceiver;


@Override
protected void onEnable() {

    IntentFilter filter = new IntentFilter();


    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);


    appReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED))
                Logger.i(getClass(), "App Removed");

            if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED))
                Logger.i(getClass(),"App Updated");

            if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))
                Logger.i(getClass(),"App Added");
        }
    };

    getContext().registerReceiver(appReceiver, filter);
}

@Override
protected void onDisable() {
    getContext().unregisterReceiver(appReceiver);
}

@Override
protected boolean isWakeLockedWhileRunning() {
    return false;
}

}

the Probe.Base and Probe.ContinuousProbe are FUNF project jars.

i set this permissions on the manifest:

<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />

when i use the broadcast receiver as class its work

here is the code that work:

public class AppChangedReceiver extends BroadcastReceiver {

@Override
   public void onReceive(Context context, Intent intent) {


    if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED))
        Logger.i(getClass(),"App Removed");

    if (intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED))
        Logger.i(getClass(),"App Updated");

    if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))
        Logger.i(getClass(),"App Added");

}

}

and in the manifest:

<receiver android:name =".sensors.EventBaseProbes.AppChangedReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.PACKAGE_REMOVED"/>
            <action android:name="android.intent.action.PACKAGE_CHANGED"/>
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <data android:scheme="package"/>
        </intent-filter>

the occurred is that when i use other actions its work in both ways.

for example if i replace the filter on the example to this filter

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

the receiver works fine and get called when the screen is on

are they 2 kinds of action: 1) need to be declared on manifest 2) don't need to be declared on manifest

I have been dealing with the same problem recently. Here is how you should do.

IntentFilter filter = new IntentFilter();

filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");

Since you have <data android:scheme="package"/> inside your AndroidManifest file. You should have it programatically too. Otherwise it won't work.

And lastly, you don't have to add any permission. These Broadcasts doesn't require permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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