简体   繁体   English

动态注册到广播接收器不起作用

[英]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 当我将广播接收器设置为匿名类时,他从未调用,但是当我将其设置为类并在android上声明时,它的工作正常

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. Probe.Base和Probe.ContinuousProbe是FUNF项目jar。

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 它们是两种动作吗:1)需要在清单中声明2)不需要在清单中声明

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. 由于您的AndroidManifest文件中有<data android:scheme="package"/> 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. 这些广播不需要许可。

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

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