简体   繁体   English

Android-来电广播接收器在取消注册后仍然有效

[英]Android - broadcast receiver for incoming calls stays alive after unregistering it

I have a broadcast receiver for listening to incoming call event. 我有一个广播接收器,用于收听来电事件。 It is defined programmatically not in the manifest. 它不在清单中以编程方式定义。 The purpose is to block all calls when that is chosen by the user. 目的是阻止用户选择的所有呼叫。 But after I unregister the broadcast receiver and close the service that registered it the receiver stays active and still blocks calls. 但是,在我取消注册广播接收器并关闭注册它的服务后,接收器保持活动状态并仍然阻止呼叫。 what I have tried: 我尝试过的

1) I tried implementing this receiver inside another broadcast receiver class I already have in this app and works fine...which means that after unregistering it it stops listening to the other events. 1)我尝试在此应用程序中已有的另一个广播接收器类中实现此接收器,并且工作正常……这意味着在取消注册后,它将停止侦听其他事件。 And while it stops listening to the other events it keeps listening to incoming call events. 在停止监听其他事件的同时,它仍在监听传入的呼叫事件。 (which is just utterly weird) (那完全是怪异的)

2) I tried implementing the incoming calls receiver in a seperate broacast receiver class and register it in a seperate service. 2)我尝试在单独的广播接收器类中实现呼入接收器,并将其注册在单独的服务中。 even after i kill this service it still stays alive. 即使我取消了这项服务,它仍然可以存活。 The calls receiver dies only when the whole app closes. 仅当整个应用程序关闭时,呼叫接收方才会死亡。

3)I tried the same thing using an activity instead of a service. 3)我尝试使用活动而不是服务进行相同的操作。

OnDestory is called in the case of service. 在服务的情况下调用OnDestory。 And the problem persists when using an activity as well. 使用活动时问题仍然存在。 How is this explained and how can I solve this problem??? 这是如何解释的,我该如何解决?

this is my code for the seperate broacast receiver class and seperate service implementation: 这是我的单独广播接收器类和单独服务实现的代码:

package com.android.internal.telephony;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CallBlockBroadcastReceiver extends BroadcastReceiver{
    BroadcastReceiver CallBlocker;
     TelephonyManager telephonyManager;
     ITelephony telephonyService;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         // TODO Auto-generated method stub
         String number=intent.getExtras().getString("incoming_number");
         Toast.makeText(context, number, Toast.LENGTH_SHORT).show();
         telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
         //Java Reflections
         Class c = null;

         try {  c = Class.forName(telephonyManager.getClass().getName());} 
         catch (ClassNotFoundException e) {     e.printStackTrace();    }
         Method m = null;

         try {  m = c.getDeclaredMethod("getITelephony");} 
         catch (SecurityException e) {      e.printStackTrace();    } 
         catch (NoSuchMethodException e) {  e.printStackTrace();    }

         m.setAccessible(true);

         try {  telephonyService = (ITelephony)m.invoke(telephonyManager);} 
         catch (IllegalArgumentException e) {e.printStackTrace();} 
         catch (IllegalAccessException e) {e.printStackTrace();} 
         catch (InvocationTargetException e) {e.printStackTrace();}

         telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
     PhoneStateListener callBlockListener = new PhoneStateListener(){
         public void onCallStateChanged(int state, String incomingNumber){
             if(state==TelephonyManager.CALL_STATE_RINGING){
                     try {
                         telephonyService.endCall();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }

             }
         }
     };

}

ITelephony class: 电话课:

package com.android.internal.telephony;
interface ITelephony {

 boolean endCall();

 void answerRingingCall();

 void silenceRinger();
}

my service 我的服务

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import com.android.internal.telephony.CallBlockBroadcastReceiver;

public class BroadcastService extends Service{
CallBlockBroadcastReceiver callBlockBroadcastReceiver = new CallBlockBroadcastReceiver();
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

            getBaseContext().registerReceiver(callBlockBroadcastReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));


        return START_STICKY;

    }

    @Override
    public void onDestroy() {

            getBaseContext().unregisterReceiver(callBlockBroadcastReceiver);


        // TODO Auto-generated method stub
        super.onDestroy();
    }


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

I would guess that onDestroy() is not getting called in your service. 我猜想onDestroy()在您的服务中没有被调用。 Are there any bindings left to the service? 服务是否还有任何绑定? Are you sure you're calling stopService(intent) on the service? 您确定要在服务上调用stopService(intent)吗?

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

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