红色:未接电话和挂断
1、权限:
private void initPhonePermissions() {
RxPermissions permissions = new RxPermissions(this);
permissions.request(
Manifest.permission.READ_CALL_LOG
).subscribe(new BaseObserver<Boolean>(context, false) {
@Override
public void receiveResult(Boolean result) {
if (result) {
} else {
ToastUtil.showShortToast("请授予相应的权限,否则无法使用");
}
}
});
}
2、获取通话记录的类
public class CallHistoryHelper {
public static String getCallHistoryStr(Context context) {
String callHistoryJson = "";
callHistoryJson= GetHostCommlog(context);
return callHistoryJson;
}
private static JSONObject GetData(Cursor aCursor) {
String date = TimeUtils.longToString(Long.parseLong(aCursor.getString(aCursor.getColumnIndex("date"))), TimeUtils.FORMAT_DATE_TIME_FULL);
Long duration=aCursor.getLong(aCursor.getColumnIndex("duration"));
String name = aCursor.getString(aCursor.getColumnIndex("name"));
String type = aCursor.getString(aCursor.getColumnIndex("type"));
if(name==null||name.equals(""))
{
name = aCursor.getString(aCursor
.getColumnIndex("number"));
}
String number=aCursor.getString(aCursor
.getColumnIndex("number"));
JSONObject obj=new JSONObject();
try {
obj.put("date", date);
obj.put("duration", duration);
obj.put("name", name);
obj.put("number", number);
obj.put("type", type);
} catch (JSONException e) {
e.printStackTrace();
}
return obj;
}
private static String GetHostCommlog(
Context iContext) {
JSONArray AllHistory=new JSONArray();
Cursor cursor = iContext.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
new String[] { "number", "name", "type", "date","duration" }, null, null,
"date DESC");
while (cursor.moveToNext()) {
AllHistory.put(GetData(cursor));
}
cursor.close();
return AllHistory.toString();
}
}
3、获取列表
String callHistoryStr = CallHistoryHelper.getCallHistoryStr(context);
List<CallHistoryBean> callHistoryList = JSON.parseArray(callHistoryStr, CallHistoryBean.class);
LogUtil.d("allContactsList:" + JSON.toJSONString(callHistoryList));
4、type值
if (type.equals("1")) {
} else if (type.equals("2")) {//已拨
} else if (type.equals("3")) {//未接
} else if (type.equals("5")) {//挂断
} else {
}