Android的 - 如何注销在清单中创建一个接收器? [英] Android - how to unregister a receiver created in the manifest?
问题描述
我知道如何用Java code。使用registerReceiver和unregisterReceiver处理接收机,但让我们说,我有我的清单如下:
I know about using registerReceiver and unregisterReceiver in Java code for dealing with receivers, but let's say I have the following in my manifest:
<receiver android:name=".headsetHook">
<intent-filter android:priority="99999999999">
<action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
</intent-filter>
</receiver>
有没有一种方法,我可以注销这个地方在Java中code?我可以给它一个id属性或东西,然后把它和注销呢?我问,因为我想我的应用程序做一些只有在第一次这个动作发生,那么注销并随后在Java中重新注册。
Is there a way I could unregister this somewhere in Java code? Could I give it an id attribute or something and then get it and unregister it? I ask because I want my app to do something only on the first time this action happens, then unregister it and re-register it later in Java.
希望我说的很清楚,感谢您的帮助。
Hope I made that clear, thanks for any help.
推荐答案
您可以使用 PackageManager
启用/禁用的BroadcastReceiver
在清单中声明。该广播接收器
将得到只有当它被启用解雇了。
You can use the PackageManager
to enable/disable a BroadcastReceiver
in declared in the Manifest. The Broadcast Receiver
will get fired only when it is enabled.
使用此创建组件
ComponentName component = new ComponentName(context, MyReceiver.class);
检查组件已启用或禁用
Check if the Component is enabled or disabled
int status = context.getPackageManager().getComponentEnabledSetting(component);
if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
Log.d("receiver is enabled");
} else if(status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Log.d("receiver is disabled");
}
启用/禁用(在你的案件广播接收器)的组件
Enable/Disable the component(Broadcast Receiver in your case)
//Disable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
//Enable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);
这篇关于Android的 - 如何注销在清单中创建一个接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!