Android获取手机通讯记录

 StringBuilder smsBuilder1 = new StringBuilder();

String str = null;
  String strNumber,strName = "";
     int type;
     long callTime;
     Date date;
     String time= "";
     ContentResolver cr = getContentResolver();
     final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
                              new String[]{CallLog.Calls.NUMBER,CallLog.Calls.CACHED_NAME,CallLog.Calls.TYPE, CallLog.Calls.DATE,CallLog.Calls.DURATION},
                              null, null,CallLog.Calls.DEFAULT_SORT_ORDER);
     cursor.moveToPosition(0);
     do{  
      strNumber = cursor.getString(0);    //呼叫号码
      strName = cursor.getString(1);   //联系人姓名
      type = cursor.getInt(2);  //来电:1,拨出:2,未接:3 public static final int INCOMING_TYPE = 1;   public static final int OUTGOING_TYPE = 2;   public static final int MISSED_TYPE = 3;
      if(type == 1){
       str = "来电";
      }else if(type == 2){
       str = "拨出";
      }else if(type == 3){
       str = "未接";
      }
      long duration = cursor.getLong(4);  //通话时间
      SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      date = new Date(Long.parseLong(cursor.getString(3)));
      time = sfd.format(date);
      smsBuilder1.append(strNumber+",");
      smsBuilder1.append(strName+",");
      smsBuilder1.append(str+",");
      smsBuilder1.append(duration+",");
      smsBuilder1.append(time+" ");
        }while(cursor.moveToNext());
    
        textView2.setText(smsBuilder1.toString());

添加权限:

<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

原文地址:https://www.cnblogs.com/xiao-xu/p/3443721.html