Android携帯電話の通話記録を読み取る
12787 ワード
import android.Manifest;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.support.v4.app.ActivityCompat;
import com.feilu.flashloan.callback.OnCallLogListener;
import com.feilu.flashloan.ui.usercenter.bean.CallLogBean;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by fengpeihao on 2017/8/23.
*
*/
public class CallLogUtils {
private static CallLogUtils mCallLogUtils;
private OnCallLogListener mListener;
public static CallLogUtils with() {
if (mCallLogUtils == null) {
synchronized (ContactUtils.class) {
mCallLogUtils = new CallLogUtils();
}
}
return mCallLogUtils;
}
public void getCallLogs(Activity activity, OnCallLogListener listener) {
mListener = listener;
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
if (mListener != null) mListener.onFailed();
return;
}
MyAsyncQueryhandler asyncQueryHandler = new MyAsyncQueryhandler(activity.getContentResolver());
Uri uri = CallLog.Calls.CONTENT_URI;
String[] projection = {"name", "number", "type", "date", "duration"};
asyncQueryHandler.startQuery(0, null, uri, projection, null, null,
CallLog.Calls.DEFAULT_SORT_ORDER);
}
private String getType(String type) {
switch (type) { // 1/ 2/ 3
case "1":
return " ";
case "2":
return " ";
case "3":
return " ";
default:
return "";
}
}
private String getDate(String date) {
Date callDate = new Date(Long.parseLong(date));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(callDate);
}
private String getDuration(String duration) {
int callDuration = NumberUtils.getInteger(duration);
if (callDuration <= 0) return "00:00:00";
int h = callDuration / 3600;
int m = callDuration % 3600 / 60;
int s = callDuration % 60;
String hour = h + "";
String min = m + "";
String sec = s + "";
if (h < 10) {
hour = "0" + h;
}
if (m < 10) {
min = "0" + m;
}
if (s < 10) {
sec = "0" + s;
}
return hour + ":" + min + ":" + sec;
}
private class MyAsyncQueryhandler extends AsyncQueryHandler {
public MyAsyncQueryhandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor != null && cursor.getCount() > 0) {
List list = new ArrayList<>();
while (cursor.moveToNext()) {
String callName = cursor.getString(cursor.getColumnIndex("name"));//
String callNumber = cursor.getString(cursor.getColumnIndex("number"));// ( , null)
String callType = cursor.getString(cursor.getColumnIndex("type"));// 1 2 3
String date = getDate(cursor.getString(cursor.getColumnIndex("date")));//
String duration = getDuration(cursor.getString(cursor.getColumnIndex("duration")));//
CallLogBean callLogBean = new CallLogBean(date, callNumber, callName, duration, callType);
list.add(callLogBean);
}
Gson gson = new Gson();
mListener.onSuccess(gson.toJson(list));
} else {
if (mListener != null)
mListener.onFailed();
}
super.onQueryComplete(token, cookie, cursor);
}
}
}
public interface OnCallLogListener { void onSuccess(String json); void onFailed(); }
:
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
6.0