Files
tianrunCRM/Assets/trCRM/Plugins/Android/java/com/coolape/tianrun/CallLogUtl.java
2020-12-01 23:32:28 +08:00

48 lines
1.5 KiB
Java

package com.coolape.tianrun;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class CallLogUtl {
public static List<CallLogInfo> getCallLog(Context context) {
List<CallLogInfo> infos = new ArrayList<CallLogInfo>();
ContentResolver cr = context.getContentResolver();
Uri uri = CallLog.Calls.CONTENT_URI;
String[] projection = new String[] { CallLog.Calls.NUMBER, CallLog.Calls.DATE,
CallLog.Calls.TYPE };
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
Cursor cursor = cr.query(uri, projection, null, null, null);
while (cursor.moveToNext()) {
String number = cursor.getString(0);
long date = cursor.getLong(1);
int type = cursor.getInt(2);
infos.add(new CallLogInfo(number, format.format(date), type));
}
cursor.close();
return infos;
}
public static String getCallLogJson(Context context) {
List<CallLogInfo> infos = getCallLog(context);
JSONArray array = new JSONArray();
for(int i=0; i < infos.size(); i++){
array.put(infos.get(i).toJson());
}
return array.toString();
}
}