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

六月ゝ 毕业季﹏ 提交于 2019-12-11 01:14:23

问题


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?


回答1:


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



回答2:


I doubt you can prevent the call from showing up in the call log. Instead of preventing the call from getting added to the log try opening the call log and deleting it.



来源:https://stackoverflow.com/questions/8216920/how-to-stop-phone-calls-to-be-log-in-the-call-log-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!