【发布时间】:2014-01-30 00:24:16
【问题描述】:
我有一个用于监听来电事件的广播接收器。它是在清单中以编程方式定义的。目的是在用户选择时阻止所有呼叫。但是在我取消注册广播接收器并关闭注册它的服务后,接收器保持活动状态并仍然阻止调用。 我尝试过的:
1) 我尝试在此应用程序中已有的另一个广播接收器类中实现此接收器,并且工作正常……这意味着在取消注册后它会停止侦听其他事件。当它停止监听其他事件时,它会继续监听来电事件。 (这太奇怪了)
2) 我尝试在单独的广播接收器类中实现来电接收器并将其注册到单独的服务中。即使我杀死了这项服务,它仍然活着。 仅当整个应用程序关闭时,呼叫接收者才会死亡。
3)我尝试使用活动而不是服务来做同样的事情。
在服务的情况下调用 OnDestory。并且在使用活动时问题仍然存在。 这是如何解释的,我该如何解决这个问题???
这是我的单独广播接收器类和单独服务实现的代码:
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 类:
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
我的服务
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;
}
}
【问题讨论】:
-
onDestroy 被调用。我检查了日志语句
标签: android broadcastreceiver call block