Get Last Call Duration in android

后端 未结 9 1566
北荒
北荒 2020-12-03 15:20

I am looking for an easiest way to get call duration of last dialed number. So for e.g if I have made a call to my mom once I cut the call a notification with the duration s

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 15:55

    public class Home extends Activity {
        TextView textView = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home_activity);
            textView = (TextView) findViewById(R.id.textview_call);
            getCallDetails();
        }
    
        private void getCallDetails() {
    
            Context context;
            StringBuffer sb = new StringBuffer();
            Uri contacts = CallLog.Calls.CONTENT_URI;
            try {
                Cursor managedCursor = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
                int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
                sb.append("Call Details :");
                while (managedCursor.moveToNext()) {
    
                    HashMap rowDataCall = new HashMap();
    
                    String phNumber = managedCursor.getString(number);
                    String callType = managedCursor.getString(type);
                    String callDate = managedCursor.getString(date);
                    String callDayTime = new Date(Long.valueOf(callDate)).toString();
                    // long timestamp = convertDateToTimestamp(callDayTime);
                    String callDuration = managedCursor.getString(duration);
                    String dir = null;
                    int dircode = Integer.parseInt(callType);
                    switch (dircode) {
                        case CallLog.Calls.OUTGOING_TYPE:
                            dir = "OUTGOING";
                            break;
    
                        case CallLog.Calls.INCOMING_TYPE:
                            dir = "INCOMING";
                            break;
    
                        case CallLog.Calls.MISSED_TYPE:
                            dir = "MISSED";
                            break;
                    }
                    sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
                    sb.append("\n----------------------------------");
    
    
                }
                managedCursor.close();
                System.out.println(sb);
                textView.setText(sb);
            }
            catch (SecurityException e)
            {
                System.out.println();
                 // lets the user know there is a problem with the code
            }
    
    
    
    
        }
    }
    

提交回复
热议问题