Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve missed calls on Android SDK 2.2

in my app I should do some action when a call comes but not answered by the user.

I have searched in the android.telephony and the NotificationManager, but I haven't found a method to solve this problem.

Does someone have an idea of how to get to know if there is a missed call on the phone or not ?

like image 925
Mathieu Avatar asked Sep 24 '10 09:09

Mathieu


People also ask

Why can't I see missed calls on Android?

If your DND option is switched on the phone won't show you missed call notification. To check bring down the notification hub and check for the do not disturb option and make sure it is turned off.


1 Answers

Here is code that can query the call log for a missed call. Basically, you will have to trigger this somehow and make sure that you give the call log some time ( a few seconds should do it) to write the information otherwise if you check the call log too soon you will not find the most recent call.

final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cursor = null;
try{
    cursor = context.getContentResolver().query(
            Uri.parse("content://call_log/calls"),
            projection,
            selection,
            selectionArgs,
            sortOrder);
    while (cursor.moveToNext()) { 
        String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
        String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
        String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
        String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
        if(Integer.parseInt(callType) == MISSED_CALL_TYPE && Integer.parseInt(isCallNew) > 0){
            if (_debug) Log.v("Missed Call Found: " + callNumber);
        }
    }
}catch(Exception ex){
    if (_debug) Log.e("ERROR: " + ex.toString());
}finally{
    cursor.close();
}

I hope you find this useful.

like image 58
Camille Sévigny Avatar answered Sep 18 '22 23:09

Camille Sévigny