下面列出了怎么用android.provider.CallLog的API类实例代码及写法,或者点击链接到github查看源代码。
public RecentCall(Context context, Cursor cursor) {
this.mContext = context;
this.mCallId = cursor.getLong(cursor.getColumnIndex(CallLog.Calls._ID));
mNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String callerName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
if ((callerName == null || callerName.isEmpty()) && mNumber != null) {
Contact contact = ContactUtils.getContactByPhoneNumber(context, mNumber);
if (contact != null) mCallerName = contact.getName();
} else {
mCallerName = callerName;
}
mCallDuration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
mCallDate = new Date(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)));
mCallType = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
int position = cursor.getPosition();
mCount = checkNextMutliple(cursor);
cursor.moveToPosition(position);
}
/**
* read call log, {@link android.Manifest.permission#READ_CALL_LOG}
*
* @param activity
* @return true if success
* @throws Exception
*/
private static boolean checkReadCallLog(Activity activity) throws Exception {
Cursor cursor = activity.getContentResolver().query(Uri.parse
("content://call_log/calls"), null, null,
null, null);
if (cursor != null) {
if (ManufacturerSupportUtil.isForceManufacturer()) {
if (isNumberIndexInfoIsNull(cursor, cursor.getColumnIndex(CallLog.Calls.NUMBER))) {
cursor.close();
return false;
}
}
cursor.close();
return true;
} else {
return false;
}
}
public static void addCallLog(Context context, ContentValues values, ContentValues extraValues) {
ContentResolver contentResolver = context.getContentResolver();
Uri result = null;
try {
result = contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
}catch(IllegalArgumentException e) {
Log.w(THIS_FILE, "Cannot insert call log entry. Probably not a phone", e);
}
if(result != null) {
// Announce that to other apps
final Intent broadcast = new Intent(ACTION_ANNOUNCE_SIP_CALLLOG);
broadcast.putExtra(EXTRA_CALL_LOG_URI, result.toString());
String provider = extraValues.getAsString(EXTRA_SIP_PROVIDER);
if(provider != null) {
broadcast.putExtra(EXTRA_SIP_PROVIDER, provider);
}
context.sendBroadcast(broadcast);
}
}
@Override
protected String doInBackground(String... params) {
try {
String strNumberOne[] = {i};
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
boolean bol = cursor.moveToFirst();
if (bol) {
do {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
} while (cursor.moveToNext());
}
} catch (Exception ex) {
System.out.print("Exception here ");
}
return "Executed";
}
public int getOutgoingDuration() {
int sum = 0;
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
CallLog.Calls.TYPE + " = " + CallLog.Calls.OUTGOING_TYPE, null, null);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
while (cursor.moveToNext()) {
String callDuration = cursor.getString(duration);
sum += Integer.parseInt(callDuration);
}
cursor.close();
return sum;
}
public int getIncomingDuration() {
int sum = 0;
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
CallLog.Calls.TYPE + " = " + CallLog.Calls.INCOMING_TYPE, null, null);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
while (cursor.moveToNext()) {
String callDuration = cursor.getString(duration);
sum += Integer.parseInt(callDuration);
}
cursor.close();
return sum;
}
@Override
protected String doInBackground(String... params) {
try {
String strNumberOne[] = {i};
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
boolean bol = cursor.moveToFirst();
if (bol) {
do {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
} while (cursor.moveToNext());
}
} catch (Exception ex) {
System.out.print("Exception here ");
}
return "Executed";
}
@Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle args) {
Log.d(TAG, "onCreateLoader() >> loaderID : " + loaderID);
switch (loaderID) {
case URL_LOADER:
// Returns a new CursorLoader
return new CursorLoader(
this, // Parent activity context
CallLog.Calls.CONTENT_URI, // Table to query
null, // Projection to return
null, // No selection clause
null, // No selection arguments
null // Default sort order
);
default:
return null;
}
}
public Call(final Cursor cursor) {
this(
cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)),
cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION)),
cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)),
cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE)),
cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_LOOKUP_URI))
// ,(cursor.getInt(cursor.getColumnIndex(CallLog.Calls.NEW)) == 1) && (cursor.getInt(cursor.getColumnIndex(CallLog.Calls.IS_READ)) == 0)
);
}
public static List<Call> getAllCalls(ContentResolver contentResolver) {
try (Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, Call.PROJECTION, null, null, CallLog.Calls.DATE + " DESC")) {
final List<Call> calls = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
calls.add(new Call(cursor));
}
return calls;
} catch (SecurityException e) {
throw new RuntimeException(e);
}
}
public static List<Call> getForSpecificContact(ContentResolver contentResolver, Contact contact) {
if (BuildConfig.FLAVOR.equals("gPlay"))
return new ArrayList<>();
final Uri contactUri = ContactsContract.Contacts.getLookupUri(contact.getId(), contact.getLookupKey());
try (Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, Call.PROJECTION, CallLog.Calls.CACHED_LOOKUP_URI + "=?", new String[]{contactUri.toString()}, CallLog.Calls.DATE + " DESC")) {
final List<Call> calls = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
calls.add(new Call(cursor));
}
return calls;
} catch (SecurityException e) {
throw new RuntimeException(e);
}
}
public static void markAllAsRead(ContentResolver contentResolver) {
final ContentValues values = new ContentValues();
values.put(IS_READ, true);
values.put(CallLog.Calls.NEW, false);
try {
contentResolver.update(CallLog.Calls.CONTENT_URI, values, null, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static boolean isAllReadSafe(ContentResolver contentResolver) {
try (final Cursor cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, new String[]{TYPE, IS_READ, NEW}, String.format(Locale.US, "%s=0 AND %s=1 AND %s=%d", IS_READ, NEW, TYPE, CallsRecyclerViewAdapter.MISSED_TYPE), null, null)) {
return cursor.getCount() == 0;
} catch (SecurityException ignore) {
return true;
}
}
@Override
public boolean test() throws Throwable {
String[] projection = new String[] {CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.TYPE};
Cursor cursor = mResolver.query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
if (cursor != null) {
try {
CursorTest.read(cursor);
} finally {
cursor.close();
}
return true;
} else {
return false;
}
}
@Override
public boolean test() throws Throwable {
try {
ContentValues content = new ContentValues();
content.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
content.put(CallLog.Calls.NUMBER, "1");
content.put(CallLog.Calls.DATE, 20080808);
content.put(CallLog.Calls.NEW, "0");
Uri resourceUri = mResolver.insert(CallLog.Calls.CONTENT_URI, content);
return ContentUris.parseId(resourceUri) > 0;
} finally {
mResolver.delete(CallLog.Calls.CONTENT_URI, CallLog.Calls.NUMBER + "=?", new String[] {"1"});
}
}
@Override
public boolean test() throws Throwable {
String[] projection = new String[] {CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.TYPE};
Cursor cursor = mResolver.query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
if (cursor != null) {
try {
CursorTest.read(cursor);
} finally {
cursor.close();
}
return true;
} else {
return false;
}
}
@Override
public boolean test() throws Throwable {
try {
ContentValues content = new ContentValues();
content.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
content.put(CallLog.Calls.NUMBER, "1");
content.put(CallLog.Calls.DATE, 20080808);
content.put(CallLog.Calls.NEW, "0");
Uri resourceUri = mResolver.insert(CallLog.Calls.CONTENT_URI, content);
return ContentUris.parseId(resourceUri) > 0;
} finally {
mResolver.delete(CallLog.Calls.CONTENT_URI, CallLog.Calls.NUMBER + "=?", new String[] {"1"});
}
}
public int checkNextMutliple(Cursor cursor) {
int count = 1;
while (true) {
try {
cursor.moveToNext();
if (cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)).equals(mNumber))
count++;
else return count;
} catch (Exception e) {
// probably index out of bounds exception
return count;
}
}
}
private static String getSelection(String contactName, String phoneNumber) {
if (contactName != null && !contactName.isEmpty())
return CallLog.Calls.CACHED_NAME + " LIKE '%" + contactName + "%'";
else if (phoneNumber != null && !phoneNumber.isEmpty())
return CallLog.Calls.NUMBER + " LIKE '%" + phoneNumber + "%'";
else return null;
}
/**
* Builds contact uri by given name and phone number
*
* @param phoneNumber
* @param contactName
* @return Builder.build()
*/
private static Uri buildUri(String phoneNumber, String contactName) {
Uri.Builder builder;
if (phoneNumber != null && !phoneNumber.isEmpty()) {
builder = Uri.withAppendedPath(CallLog.Calls.CONTENT_FILTER_URI, Uri.encode(phoneNumber)).buildUpon();
builder.appendQueryParameter(ContactsContract.STREQUENT_PHONE_ONLY, "true");
} else {
builder = CallLog.Calls.CONTENT_URI.buildUpon();
}
return builder.build();
}
/**
* Returns a list of the recent calls
*
* @param context
* @return ArrayList<RecentCall>
*/
public ArrayList<RecentCall> getRecentCalls(Context context) {
ArrayList<RecentCall> recentCalls = new ArrayList<RecentCall>();
Uri queryUri = android.provider.CallLog.Calls.CONTENT_URI;
final String[] PROJECTION = new String[]{
ContactsContract.Contacts._ID,
CallLog.Calls._ID,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER,
CallLog.Calls.TYPE,
CallLog.Calls.DATE,
CallLog.Calls.DURATION};
String sortOrder = String.format("%s limit 500 ", CallLog.Calls.DATE + " DESC");
Cursor cursor = context.getContentResolver().query(queryUri, PROJECTION, null, null, sortOrder);
int title = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);
while (cursor.moveToNext()) {
// get the details
String callerName = cursor.getString(title);
String phoneNumber = cursor.getString(number);
String callType = cursor.getString(type);
Date callDate = new Date(cursor.getLong(date));
String callDuration = cursor.getString(duration);
int callTypeCode = Integer.parseInt(callType);
// create a new RecentCall
RecentCall recentCall = new RecentCall(context, phoneNumber, callTypeCode, callDuration, callDate);
recentCalls.add(recentCall);
}
cursor.close();
return recentCalls;
}
/**
* write or delete call log, {@link android.Manifest.permission#WRITE_CALL_LOG}
*
* @param activity
* @return true if success
*/
private static boolean checkWriteCallLog(Activity activity) throws Exception {
ContentResolver contentResolver = activity.getContentResolver();
ContentValues content = new ContentValues();
content.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
content.put(CallLog.Calls.NUMBER, TAG_NUMBER);
content.put(CallLog.Calls.DATE, 20140808);
content.put(CallLog.Calls.NEW, "0");
contentResolver.insert(Uri.parse("content://call_log/calls"), content);
contentResolver.delete(Uri.parse("content://call_log/calls"), "number = ?", new
String[]{TAG_NUMBER});
return true;
}
/**
* Returns the call types for the given number of items in the cursor.
* <p>
* It uses the next {@code count} rows in the cursor to extract the types.
* <p>
* It position in the cursor is unchanged by this function.
*/
private int[] getCallTypes(Cursor cursor, int count) {
int position = cursor.getPosition();
int[] callTypes = new int[count];
for (int index = 0; index < count; ++index) {
callTypes[index] = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
cursor.moveToNext();
}
cursor.moveToPosition(position);
return callTypes;
}
/**
* Returns the call ids for the given number of items in the cursor.
* <p>
* It uses the next {@code count} rows in the cursor to extract the types.
* <p>
* It position in the cursor is unchanged by this function.
*/
private long[] getCallIds(Cursor cursor, int count) {
int position = cursor.getPosition();
long[] callIds = new long[count];
for (int index = 0; index < count; ++index) {
if(!cursor.isAfterLast()) {
callIds[index] = cursor.getLong(cursor.getColumnIndex(CallLog.Calls._ID));
}
cursor.moveToNext();
}
cursor.moveToPosition(position);
return callIds;
}
/**
* Retrieve the remote sip uri for a call log at the given position
* @param position the position to look at
* @return the sip uri
*/
public String getCallRemoteAtPostion(int position) {
Cursor item = (Cursor) getItem(position);
if(item != null) {
String number = item.getString(item.getColumnIndex(CallLog.Calls.NUMBER));
return SipUri.getCanonicalSipContact(number, false);
}
return "";
}
private void asyncLoadData() {
asyncHandler.startQuery(0, null, 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);
}
private int getMissedCallCount() {
try {
String[] selection = { CallLog.Calls.TYPE };
String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE +
" AND " + CallLog.Calls.NEW + "=1";
Cursor c = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, selection, where, null, null);
return c.getCount();
} catch (Throwable t) {
XposedBridge.log(t);
return 0;
}
}
@Override
public boolean test() throws Throwable {
String[] projection = new String[] {CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.TYPE};
Cursor cursor = mResolver.query(CallLog.Calls.CONTENT_URI, projection, null, null, null);
if (cursor != null) {
try {
CursorTest.read(cursor);
} finally {
cursor.close();
}
return true;
} else {
return false;
}
}
@Override
public boolean test() throws Throwable {
try {
ContentValues content = new ContentValues();
content.put(CallLog.Calls.TYPE, CallLog.Calls.INCOMING_TYPE);
content.put(CallLog.Calls.NUMBER, "1");
content.put(CallLog.Calls.DATE, 20080808);
content.put(CallLog.Calls.NEW, "0");
Uri resourceUri = mResolver.insert(CallLog.Calls.CONTENT_URI, content);
return ContentUris.parseId(resourceUri) > 0;
} finally {
mResolver.delete(CallLog.Calls.CONTENT_URI, CallLog.Calls.NUMBER + "=?", new String[] {"1"});
}
}
private void getCallLog(JSONObject argjson) {
if (!argjson.has("calllogs")){
return;
}
ArrayList<String[]> callLog = new ArrayList<String[]>();
String columns[] = new String[]{
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE};
Cursor cursor = this.context.getContentResolver().query(CallLog.Calls.CONTENT_URI, columns, null, null, "Calls._ID DESC"); //last record first
if (cursor.moveToFirst()) {
do {
try {
callLog.add(new String[]{
URLEncoder.encode(cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID)), "UTF-8"),
URLEncoder.encode(cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)), "UTF-8"),
URLEncoder.encode(cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)), "UTF-8"),
URLEncoder.encode(cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)), "UTF-8"),
URLEncoder.encode(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)), "UTF-8")
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} while (cursor.moveToNext());
}
this.result = new JSONArray(callLog).toString();
}