How to count the number of missed calls in Android

I know that how to get missed call alerts, but I do not know how many number of missed calls I have. So how can I count this number of missed calls?

I am using this code in oncreate:

TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
       TelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE);

After that I am using this code to get an alert on a missed call:

class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if (ring == true && callReceived == false) {
missedcallno=incomingNumber;
Toast.makeText(getApplicationContext(),"It was A MISSED CALL:" + incomingNumber,Toast.LENGTH_LONG).show();
}
default:
break;
}
}
}

Upvotes: 0

Views: 2037

Answers (1)

Govinda P
Govinda P

Reputation: 3319

Check this answer: https://stackoverflow.com/a/7621988/5275436

For Missed calls:

String[] projection = { CallLog.Calls.CACHED_NAME, CallLog.Calls.CACHED_NUMBER_LABEL, CallLog.Calls.TYPE };
           String where = CallLog.Calls.TYPE+"="+CallLog.Calls.MISSED_TYPE;          
           Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI, projection,where, null, null);
           c.moveToFirst();    
           Log.d("CALL", ""+c.getCount()); //do some other operation

and declare permission in manifest :

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

Detail about selection and selectionArgs

selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.

selectionArgs (where) You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.

Upvotes: 2

Related Questions