启用和禁用的广播接收器 [英] Enable and disable a Broadcast Receiver
问题描述
我尝试启用和使用该PackageManager方法禁用广播接收器:
setComponentEnabledSetting(组件名,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
广播接收器被登记在清单中。接收器工作正常,但是当我尝试禁用它,它仍然会接收广播消息。 当我禁用清单中的接收器通过安卓启用=FALSE,接收方没有收到任何东西,但我无法启用它
我从服务中调用该方法。
PackageManager PM = getApplicationContext()getPackageManager()。
组件名组件名=新的组件名(com.app
.broadcast_receivers.OnNetworkChangedReceiver);
pm.setComponentEnabledSetting(组件名,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Android清单:
<接收机器人:名称=。broadcast_receivers.OnNetworkChangedReceiver
机器人:启用=真正的>
<意向滤光器>
<作用机器人:名称=android.net.conn.CONNECTIVITY_CHANGE/>
&所述; /意图滤光器>
< /接收器>
该接收器
OnNetworkChangedReceiver延伸的BroadcastReceiver {public类
私有静态最后字符串变量=OnNetworkChangedReceiver;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.d(TAG,在OnNetworkChanged);
}
}
我还调用的方法从活动里昨天。我认为它的工作,但现在没有工作了。 难道说有时会有很大的延迟的意图(android.net.conn.CONNECTIVITY_CHANGE),我misinter昨天PTED为禁用接收器$ P $?
时的PackageManager的方法正确的方向还是有一个基本的错误的想法?
非常感谢, 斯文
那么,你基本上似乎确定。我在我的项目之一的:
布尔启用= prefs.getBoolean(键,FALSE);
INT标志=(启用?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
组件名组件=新的组件名(编辑preferences.this,OnBootReceiver.class);
getPackageManager()
.setComponentEnabledSetting(组件,标志,
PackageManager.DONT_KILL_APP);
我用这个来禁用 BOOT_COMPLETED
接收器时,我没有这方面的需要安排任何活动的警报。
我的猜测是,你的组件名
设置不正确(例如,你的领导。
)。试试,我使用的构造,这需要一个上下文
和类
作为参数。</ P>
I try to enable and disable a broadcast receiver by using this PackageManager method:
setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
The broadcast receiver is registered in the manifest. The receiver works fine but when i try to disable it, it still receives the broadcast messages. When i disable the receiver in the manifest by "android:enabled="false"", the receiver does not receive anything but I can not enable it.
I call the method from inside a service.
PackageManager pm = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName("com.app",
".broadcast_receivers.OnNetworkChangedReceiver");
pm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Android manifest:
<receiver android:name=".broadcast_receivers.OnNetworkChangedReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
The Receiver
public class OnNetworkChangedReceiver extends BroadcastReceiver {
private static final String TAG = "OnNetworkChangedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "in OnNetworkChanged");
}
}
I also called the method from inside an Activity yesterday. I thought it worked but today nothing works anymore. Could it be that there is sometimes a big delay in the intent (android.net.conn.CONNECTIVITY_CHANGE) that I misinterpreted yesterday as disabling the receiver?
Is the approach with the PackageManager the right direction or is there a basic error in the idea?
Thanks a lot, Sven
Well, what you basically have seems OK. I have the following code in one of my projects:
boolean enabled=prefs.getBoolean(key, false);
int flag=(enabled ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
ComponentName component=new ComponentName(EditPreferences.this, OnBootReceiver.class);
getPackageManager()
.setComponentEnabledSetting(component, flag,
PackageManager.DONT_KILL_APP);
I use this to disable a BOOT_COMPLETED
receiver when I don't have any active alarms that need to be scheduled.
My guess is that your ComponentName
is not set up properly (e.g., your leading .
). Try the constructor that I am using, that takes a Context
and a Class
as parameters.
这篇关于启用和禁用的广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!