Naruto
Naruto

Reputation: 9634

How to know the call state in android

i know from Phonestatelistener we will get to know, the call type like its incoming, or ringing or picked etc.. But once call ends i.e once phone reaches idle state, i want to know what was the state of call, was it picked, missed or rejected

lets take, +91123456789 is an incoming call, after call ends, the number will be stored in phone call log as missed call,

is there a way to fetch the recent state from call log for the particular number +91123456789, is it possible?

Upvotes: 0

Views: 442

Answers (1)

Suhail Mehta
Suhail Mehta

Reputation: 5542

Here is code that can query the call log for a missed call. Basically, you will have to trigger this 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.

int MISSED_CALL_TYPE = android.provider.CallLog.Calls.MISSED_TYPE
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);
cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + "=? ", yourNumber, 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);
break ;
                }
            }
        }catch(Exception ex){
            if (_debug) Log.e("ERROR: " + ex.toString());
        }finally{
            cursor.close();
        }

I hope you find this useful, in the same way you can get other states.

Do add this permission in manifest android.permission.READ_CONTACTS

Upvotes: 2

Related Questions