答案 0 :(得分:10)
<强> 1。创建观察者:
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。注册观察员:
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);
修改强> 从Jellybean(4.1)开始,您现在需要获得许可:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
为此工作而不抛出权限拒绝异常