在 IntentService 中注销 BroadcastReceiver

发布于 2024-12-22 16:17:28 字数 1972 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-12-29 16:17:28

我正在使用意图来启动 IntentService,该服务注册 BroadcastReceiver 来侦听手机状态的更改

这不会可靠地工作,因为一旦 IntentService 退出,您的进程可能会终止,并且您的 BroadcastReceiver< /code> 将消失。请将手机状态 BroadcastReceiver 放入您的清单中。

但是,我似乎无法弄清楚如何在小部件关闭时取消注册 BroadcastReceiver

使用 PackageManagersetComponentEnabledSetting() 启用或禁用手机状态接收器您放置在清单中。

此外,您无法通过 onReceive() 存在的 BroadcastReceiver 可靠地执行任何操作,例如注册事件侦听器,就像您在这里所做的那样。

I am using an intent to start an IntentService that registers a BroadcastReceiver to listen for changes the phone state

This will not work reliably, as once the IntentService exits, your process may terminate and your BroadcastReceiver will go away. Please put the phone state BroadcastReceiver in your manifest.

However, I can't seem to figure out how to unregister the BroadcastReceiver when the widget has been toggled off

Use PackageManager and setComponentEnabledSetting() to enable or disable the phone state receiver you place in the manifest.

Also, you cannot reliably do anything from a BroadcastReceiver that lives past onReceive(), such as register an event listener, as you are doing here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文