I want to read call logs in the android [duplicate]
Possible Duplicate:
How can I retrieve recently used contacts in android?
I wan开发者_如何学JAVAt to read recently used contacts.
Anyone know how to do this?
1. Create observer:
class CustomContentObserver extends ContentObserver {
public CustomContentObserver(Handler handler) {
super(handler);
}
@Override public boolean deliverSelfNotifications() {
return false;
}
public void logCallLog() {
long dialed;
String columns[]=new String[] {
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE};
Cursor c;
c = getContentResolver().query(Uri.parse("content://call_log/calls"),
columns, null, null, "Calls._ID DESC"); //last record first
while (c.moveToNext()) {
dialed=c.getLong(c.getColumnIndex(CallLog.Calls.DATE));
Log.i("CallLog","type: " + c.getString(4) + "Call to number: "+number+", registered at: "+new Date(dialed).toString());
}
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("PhoneService", "StringsContentObserver.onChange( " + selfChange + ")");
logCallLog();
}
}
2. Register observer:
Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
Log.d("PhoneService", "The Encoded path of the media Uri is "
+ mediaUri.getEncodedPath());
CustomContentObserver custObser = new CustomContentObserver(handler);
imageContentRsr.registerContentObserver(mediaUri, false, custObser);
EDIT: As of Jellybean (4.1), you now need the permission:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
For this to work and not throw a Permission Denial exception
精彩评论