Get last call details from Call Log

后端 未结 1 1030
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-10 21:37

I am using below code to get last call details from call log.

public static CallDetails getLastCallDetails(Context context) {

    CallDetails callDetails = new          


        
相关标签:
1条回答
  • 2021-02-10 22:24

    Add this line managedCursor.moveToFirst()

     public static CallDetails getLastCallDetails(Context context) {
    
            CallDetails callDetails = new CallDetails();
    
            Uri contacts = CallLog.Calls.CONTENT_URI;
            try {
    
                Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
    
                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
                int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));
    
               if(managedCursor.moveToFirst()){ // added line
    
                while (managedCursor.moveToNext()) { 
                    String callType;
                    String phNumber = managedCursor.getString(number);
                    String callerName = getContactName(context, phNumber);
                    if(incomingtype == -1){
                        callType = "incoming";
                    }
                    else {
                        callType = "outgoing";
                    }
                    String callDate = managedCursor.getString(date);
                    String callDayTime = new      Date(Long.valueOf(callDate)).toString();
    
                    String callDuration = managedCursor.getString(duration);
    
                    callDetails.setCallerName(callerName);
                    callDetails.setCallerNumber(phNumber);
                    callDetails.setCallDuration(callDuration);
                    callDetails.setCallType(callType);
                    callDetails.setCallTimeStamp(callDayTime);
    
              }
            }
                managedCursor.close();
    
            } catch (SecurityException e) {
                Log.e("Security Exception", "User denied call log permission");
    
            }
    
            return callDetails;
    
        }
    
    0 讨论(0)
提交回复
热议问题