简体   繁体   English

在IntentService中注销BroadcastReceiver

[英]Unregister BroadcastReceiver in IntentService

I'm new to Android development. 我是Android开发的新手。 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. 我首先创建了一个扩展AppWidgetProvider的类。 I'm using SharedPreferences to store the state of the widget button. 我正在使用SharedPreferences存储小部件按钮的状态。 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. 在OnReceive(Context,Intent)方法中,我正在使用一种意图来启动一个IntentService,该服务注册一个BroadcastReceiver来侦听电话状态的更改。 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. 但是,我似乎无法弄清楚在关闭小部件后如何注销BroadcastReceiver的方法-无论小部件按钮的状态如何,我的代码都将继续监视电话状态。 Is it possible? 可能吗? Or is there another way that I can stop the PhoneStateListener? 还是有其他方法可以停止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 清单:我将带有android.intent.action.PHONE_STATE intent-filter的BroadcastReceiver放在清单中

In the AppWidgetProvider: 在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: 在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... 不需要PhoneStateListener ...

Bummer that this won't work on 2.3+ 糟糕这不适用于2.3+

I am using an intent to start an IntentService that registers a BroadcastReceiver to listen for changes the phone state 我正在使用一个意图来启动一个IntentService来注册一个BroadcastReceiver来侦听电话状态的变化

This will not work reliably, as once the IntentService exits, your process may terminate and your BroadcastReceiver will go away. 这将无法可靠地运行,因为一旦退出IntentService ,您的进程可能会终止,并且BroadcastReceiver将消失。 Please put the phone state BroadcastReceiver in your manifest. 请在清单中放入电话状态BroadcastReceiver

However, I can't seem to figure out how to unregister the BroadcastReceiver when the widget has been toggled off 但是,我似乎无法弄清楚在关闭小部件后如何注销BroadcastReceiver

Use PackageManager and setComponentEnabledSetting() to enable or disable the phone state receiver you place in the manifest. 使用PackageManagersetComponentEnabledSetting()启用或禁用放置在清单中的电话状态接收器。

Also, you cannot reliably do anything from a BroadcastReceiver that lives past onReceive() , such as register an event listener, as you are doing here. 同样,您无法像在此那样,可靠地通过存在于onReceive()BroadcastReceiver进行任何操作,例如注册事件侦听器。

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

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