Search code examples
androidcalllog

how to stop phone calls to be log in the call log on android


I'm writing an application that block some phone calls. I use a broadcast receiver to listen to incoming calls:

<receiver android:name="InComingCallReceiver">
<intent-filter android:priority="100">
    <action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>

And I cancelled the calls I don't want:

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);    
try {   
    Class c = Class.forName(telephony.getClass().getName());   
    Method m = c.getDeclaredMethod("getITelephony");   
    m.setAccessible(true);   
    telephonyService = (ITelephony) m.invoke(telephony);   
    telephonyService.silenceRinger();   
    telephonyService.endCall();  
} 
catch (Exception e) {   
    Log.e(LOG_TAG,"Exception in InComingCallReceiver.onReceive");
    Log.e(LOG_TAG,"ERROR: " + e.toString() + " Message: " + e.getMessage() + " --- " + e.getLocalizedMessage() + " Cause: " + e.getCause() + " StackTrace: " + e.getStackTrace());
}

The call is indead cancelled but it appears in the call log.

Is there a way to prevent call being log in the call log after I cancelled it?

If not, how can I delete from the log programatically?


Solution

  • It Look like there is no way to prevent the system to log the phone call in the call log. So we have to delete it from the call log. The problem is that the entry is added in the call log long after the phone call has been hang up and we do not see it in the database when we are in the onReceive method of the broadcast receiver.

    After a lot of research and tests, I came up with this simple solution. I make the thread sleep for 2 seconds befor deleting it.

    Here's the code :

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(LOG_TAG, "Début InComingCallReceiver.onReceive");
        Log.i(LOG_TAG, "IS ORDERED = " + this.isOrderedBroadcast());
    
        Bundle extras = intent.getExtras();
        if (extras != null) {
    
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.i(LOG_TAG, state);
    
            String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.i(LOG_TAG, phoneNumber);
    
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
    
                if (phoneNumber.contains(PHONE_FILTER)) {
    
                    Log.i(LOG_TAG, "Cancelling the incoming call");
    
                    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);    
                    try {   
                        Class c = Class.forName(telephony.getClass().getName());   
                        Method m = c.getDeclaredMethod("getITelephony");   
                        m.setAccessible(true);   
                        telephonyService = (ITelephony) m.invoke(telephony);   
                        telephonyService.silenceRinger();   
                        telephonyService.endCall();  
    
                    } 
                    catch (Exception e) {   
                        Log.e(LOG_TAG,"Exception in InComingCallReceiver.onReceive");
                        Log.e(LOG_TAG,"ERROR: " + e.toString() + " Message: " + e.getMessage() + " --- " + e.getLocalizedMessage() + " Cause: " + e.getCause() + " StackTrace: " + e.getStackTrace());
                    }
                }
            }
            else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if (phoneNumber.contains(PHONE_FILTER)) {
    
                    Log.i(LOG_TAG, "Waiting 2sec");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.i(LOG_TAG, "After Waiting 2sec");
    
    
                    Log.i(LOG_TAG, "Deleting the incoming call from call log");
                    int nbRowDeleted = context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, CallLog.Calls.NUMBER + " = ?", new String[] {phoneNumber});
                    Log.i(LOG_TAG, nbRowDeleted + " Row(s) Deleted");
                }
            }
        }
    }