0

I would like to create a app that i can be able to set different phone numbers in a block or allow list. Is it possible to block or allow only certain phone numbers and if so how would i go about doing this. Thanks in advance, Jonathan.

2
  • contentObserver will help you Aug 6, 2011 at 7:13
  • Recieve a broadcast of calls and use telephony ISDL to end call . telephony isdl contains end call method that will help you to end calls. for particular number you have to check the number with telephony manager Aug 6, 2011 at 7:27

2 Answers 2

0

Sounds like you have to create a Service with a registered PhoneStateListener which enables you to listen to events regarding the change of the phone state. So if someone calls you can catch that event in your listener and react accordingly. Your own PhoneStateListener could look like this:

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;
    }
}

}

In the case where phone state equals CALL_STATE_RINGING you can place your logic to block the incoming call if the incoming number equals a number in your "blocked numbers list". To get it all working you have to register that listener in your service (in the onCreate method and don't forget to unregister it in onDestroy) and also add a permission to your android manifest which enables you to read the phone state.

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

first thing create a broadcast reciever: 1)add to manifest

2)the reciever:

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); 
     }
    }

}

now this will only work once you started your app at least once. (you can call your app to start a service on boot using another reciever that will call startActivity) the permisions and declaration for it are:

<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> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.