在 IntentService 中注销 BroadcastReceiver
我是 Android 开发新手。我正在尝试开发一个带有按钮的小部件,该按钮可以切换设备是否自动应答来电。
我首先创建了一个扩展 AppWidgetProvider 的类。我使用 SharedPreferences 来存储小部件按钮的状态。在 OnReceive(Context, Intent) 方法中,我使用意图来启动一个 IntentService,该服务注册一个 BroadcastReceiver 来侦听电话状态的更改。但是,我似乎无法弄清楚如何在小部件关闭时取消注册 BroadcastReceiver ——无论小部件按钮的状态如何,我的代码都会继续监视手机状态。是否可以?或者有其他方法可以停止 PhoneStateListener 吗?
谢谢。
更新:以下是对代码清单的修订
:我将带有 android.intent.action.PHONE_STATE 意图过滤器的 BroadcastReceiver 放在清单中
在 AppWidgetProvider 中:
ComponentName componentName = new ComponentName(context, PhoneStateBroadcastReceiver.class);
//use to enable the broadcast receiver
packageManager.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
//use to disable the broadcast receiver
packageManager.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
在 BroadcastReceiver 中:
@Override
public void onReceive(Context context, Intent intent) {
//check call state
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm.getCallState()==TelephonyManager.CALL_STATE_RINGING){
try{
//wait 2 seconds then answer the call
Thread.sleep(2000);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephony = (ITelephony)m.invoke(tm);
telephony.silenceRinger();
telephony.answerRingingCall();
} catch(Exception e){}
}
}
不需要PhoneStateListener...
令人遗憾的是 这在 2.3+ 上不起作用
I'm new to Android development. I am trying to develop a widget with a button that toggles whether the device automatically answers incoming calls.
I started by created a class that extends AppWidgetProvider. I'm using SharedPreferences to store the state of the widget button. In the OnReceive(Context, Intent) method, I am using an intent to start an IntentService that registers a BroadcastReceiver to listen for changes the phone state. However, I can't seem to figure out how to unregister the BroadcastReceiver when the widget has been toggled off -- my code continues to monitor the phone state no matter the state of the widget button. Is it possible? Or is there another way that I can stop the PhoneStateListener?
Thanks.
UPDATE: Here are the revisions to the code
Manifest: I put the BroadcastReceiver with the android.intent.action.PHONE_STATE intent-filter in the manifest
In the AppWidgetProvider:
ComponentName componentName = new ComponentName(context, PhoneStateBroadcastReceiver.class);
//use to enable the broadcast receiver
packageManager.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
//use to disable the broadcast receiver
packageManager.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
In the BroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
//check call state
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm.getCallState()==TelephonyManager.CALL_STATE_RINGING){
try{
//wait 2 seconds then answer the call
Thread.sleep(2000);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephony = (ITelephony)m.invoke(tm);
telephony.silenceRinger();
telephony.answerRingingCall();
} catch(Exception e){}
}
}
Don't need a PhoneStateListener...
Bummer that this won't work on 2.3+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不会可靠地工作,因为一旦
IntentService
退出,您的进程可能会终止,并且您的BroadcastReceiver< /code> 将消失。请将手机状态
BroadcastReceiver
放入您的清单中。使用
PackageManager
和setComponentEnabledSetting()
启用或禁用手机状态接收器您放置在清单中。此外,您无法通过
onReceive()
存在的BroadcastReceiver
可靠地执行任何操作,例如注册事件侦听器,就像您在这里所做的那样。This will not work reliably, as once the
IntentService
exits, your process may terminate and yourBroadcastReceiver
will go away. Please put the phone stateBroadcastReceiver
in your manifest.Use
PackageManager
andsetComponentEnabledSetting()
to enable or disable the phone state receiver you place in the manifest.Also, you cannot reliably do anything from a
BroadcastReceiver
that lives pastonReceive()
, such as register an event listener, as you are doing here.