阻止拨入​​和拨出电话?

时间:2011-08-06 07:06:00

标签: android phone-call

我想创建一个应用程序,我可以在一个块或允许列表中设置不同的电话号码。是否可以阻止或仅允许某些电话号码,如果是这样,我将如何进行此操作。在此先感谢Jonathan。

2 个答案:

答案 0 :(得分:0)

听起来您必须使用已注册的PhoneStateListener创建服务,这使您可以收听有关电话状态更改的事件。因此,如果有人打电话,您可以在听众中捕捉到该事件并做出相应的反应。您自己的PhoneStateListener可能如下所示:

public class CustomPhoneStateListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:            
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        doSomething();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        if(incomingNumber.equels(blockedNumber)) {
            blockNumber();
        }
        break;
    }
}

}

如果电话状态等于CALL_STATE_RINGING,您可以将逻辑设置为阻止来电,如果传入号码等于“阻止号码列表”中的号码。要使它全部正常工作,你必须在你的服务中注册该监听器(在onCreate方法中并且不要忘记在onDestroy中取消注册它)并且还为你的android清单添加一个权限,使你能够读取手机状态。

      <uses-permission android:name="android.permission.READ_PHONE_STATE" />

答案 1 :(得分:0)

首先创建一个广播接收器: 1)添加到清单

2)接收者:

public class CallReciever extends BroadcastReceiver 
{

@Override
public void onReceive(Context context, Intent intent) 
{
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    ArrayList<String> rejectList=getListFromDb()//you need to implement this method
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        String num = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        if (rejectList.Contains(num))
        {
            disconnectCall();


        }
    }
 public void disconnectCall(){
     try {

        String serviceManagerName = "android.os.ServiceManager";
        String serviceManagerNativeName = "android.os.ServiceManagerNative";
        String telephonyName = "com.android.internal.telephony.ITelephony";
        Class<?> telephonyClass;
        Class<?> telephonyStubClass;
        Class<?> serviceManagerClass;
        Class<?> serviceManagerNativeClass;
        Method telephonyEndCall;
        Object telephonyObject;
        Object serviceManagerObject;
        telephonyClass = Class.forName(telephonyName);
        telephonyStubClass = telephonyClass.getClasses()[0];
        serviceManagerClass = Class.forName(serviceManagerName);
        serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
        Method getService = // getDefaults[29];
        serviceManagerClass.getMethod("getService", String.class);
        Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
        Binder tmpBinder = new Binder();
        tmpBinder.attachInterface(null, "fake");
        serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
        IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
        Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
        telephonyObject = serviceMethod.invoke(null, retbinder);
        telephonyEndCall = telephonyClass.getMethod("endCall");
        telephonyEndCall.invoke(telephonyObject);

      } catch (Exception e) {
        e.printStackTrace();
        Log.e("GABI",
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e("GABI", "Exception object: " + e); 
     }
    }

}

现在只有在您启动应用至少一次后才能使用此功能。 (您可以使用另一个将调用startActivity的接收器调用您的应用程序在启动时启动服务) 其中的声明和声明是:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.my.CustomReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>