1.权限
<uses-permission android:name="android.permission.READ_SMS"/> <uses-permission android:name="android.permission.WRITE_SMS"/> <uses-permission android:name="android.permission.READ_CALL_LOG" /> <uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
2.读取短信
public String getSmsInPhone(Context context) { final String SMS_URI_ALL = "content://sms/"; final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; StringBuilder smsBuilder = new StringBuilder(); try{ ContentResolver cr = context.getContentResolver(); String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"}; Uri uri = Uri.parse(SMS_URI_ALL); Cursor cur = cr.query(uri, projection, null, null, "date desc"); if (cur.moveToFirst()) { String name; String phoneNumber; String smsbody; String date; String type; int nameColumn = cur.getColumnIndex("person"); int phoneNumberColumn = cur.getColumnIndex("address"); int smsbodyColumn = cur.getColumnIndex("body"); int dateColumn = cur.getColumnIndex("date"); int typeColumn = cur.getColumnIndex("type"); do{ name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneNumberColumn); smsbody = cur.getString(smsbodyColumn); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(cur.getString(dateColumn))); date = dateFormat.format(d); int typeId = cur.getInt(typeColumn); if(typeId == 1){ type = "接收"; } else if(typeId == 2){ type = "发送"; } else { type = ""; } smsBuilder.append("["); smsBuilder.append(name+","); smsBuilder.append(phoneNumber+","); smsBuilder.append(smsbody+","); smsBuilder.append(date+","); smsBuilder.append(type); smsBuilder.append("] "); if(smsbody == null) smsbody = ""; }while(cur.moveToNext()); } else { smsBuilder.append("no result!"); } smsBuilder.append("getSmsInPhone has executed!"); } catch(SQLiteException ex) { Log.d("SQLi", ex.getMessage()); } return smsBuilder.toString(); }
3.通讯记录读取
private List<Map<String, String>> getDataList() { // 1.获得ContentResolver ContentResolver resolver = getContentResolver(); // 2.利用ContentResolver的query方法查询通话记录数据库 /** * @param uri 需要查询的URI,(这个URI是ContentProvider提供的) * @param projection 需要查询的字段 * @param selection sql语句where之后的语句 * @param selectionArgs ?占位符代表的数据 * @param sortOrder 排序方式 * */ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return TODO; } Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.DURATION , CallLog.Calls.TYPE} , null, null, CallLog.Calls.DEFAULT_SORT_ORDER); // 3.通过Cursor获得数据 List<Map<String, String>> list = new ArrayList<Map<String, String>>(); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)); String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)); String date = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date(dateLong)); int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION)); int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)); String typeString = ""; switch (type) { case CallLog.Calls.INCOMING_TYPE: typeString = "打入"; break; case CallLog.Calls.OUTGOING_TYPE: typeString = "打出"; break; case CallLog.Calls.MISSED_TYPE: typeString = "未接"; break; default: break; } Map<String, String> map = new HashMap<String, String>(); map.put("name", (name == null) ? "未备注联系人" : name); map.put("number", number); map.put("date", date); map.put("duration", (duration / 60) + "分钟"); map.put("type", typeString); list.add(map); } return list; }
--------------------
1.