Android - remove missed call notification

2020-03-06 02:17发布

问题:

is there anyway to remove a missed call notification by code? And somehow remove the last missed call from call history?

回答1:

yes, it is possible.Try this:

Uri UriCalls = Uri.parse("content://call_log/calls");
Cursor cursor = getApplicationContext().getContentResolver().query(UriCalls, null, null, null, null);

Reading call log entries...

if(cursor.getCount() > 0){
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
        String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
        String duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));// for duration
        int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going
        cursor.moveToNext();
    }
}

Deleting entry in call log...

String queryString= "NUMBER='" + number + "'";
if (cursor.getCount() > 0){
        getApplicationContext().getContentResolver().delete(UriCalls, queryString, null);
}

Permission:

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

Note: Please refer this doc over call log for more clearity.

Using the above code you can get the desired result.



回答2:

Refer to this You can clear your missed call by calling cancel(ID) or calcelAll() to clear your notifications bar.



回答3:

For the part where you want to remove the last call from the log you need to move the method that is deleting the entry into a class which is a subclass of the Thread class. This allows you to then set it to sleep for a short period to allow Android to actually write to the call log BEFORE you you run the delete query. I had the same problem, but manage to resolve it with the code below:

public class DelayClearCallLog extends Thread {

    public Context context;
    public String phoneNumber;

     public DelayClearCallLog(Context ctx, String pNumber){

            context = ctx;
            phoneNumber = pNumber;
        }

     public void run() {
         try {
             sleep(2000);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }

         clearCallLog(context, phoneNumber);
     } 
 public void clearCallLog(Context context, String phoneNumber) {
         // implement delete query here
     }
}

Then call the the method as follows:

DelayClearCallLog DelayClear = new DelayClearCallLog(context, phoneNumber);
DelayClear.start();