How to retrieve missed calls on Android SDK 2.2

后端 未结 3 837
自闭症患者
自闭症患者 2020-12-14 04:38

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<

3条回答
  •  萌比男神i
    2020-12-14 05:24

    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.

提交回复
热议问题