2
String[] projection = new String[] {
   CallLog.Calls._ID, 
   CallLog.Calls.CACHED_NAME, 
   CallLog.Calls.DATE, 
   CallLog.Calls.DURATION, 
   CallLog.Calls.NUMBER, 
   CallLog.Calls.TYPE 
};

Cursor cursor = this.getContentResolver().query(
       android.provider.CallLog.Calls.CONTENT_URI, projection, null, null, null
);

我想从 android 通话记录中获取应用程序的通话记录。目前我一直在开发 android 4.0 版本。我尝试了上面的代码,它会生成以下异常。这是什么原因?我已将<uses-permission android:name="android.permission.READ_CONTACTS" />权限标签放置在清单文件中。

01-19 21:25:52.819: E/AndroidRuntime(3280): FATAL EXCEPTION: main
01-19 21:25:52.819: E/AndroidRuntime(3280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.phonelog/com.example.phonelog.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{41397ff8 3280:com.example.phonelog/u0a10053} (pid=3280, uid=10053) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.os.Looper.loop(Looper.java:137)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at java.lang.reflect.Method.invoke(Method.java:511)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at dalvik.system.NativeStart.main(Native Method)
01-19 21:25:52.819: E/AndroidRuntime(3280): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{41397ff8 3280:com.example.phonelog/u0a10053} (pid=3280, uid=10053) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.os.Parcel.readException(Parcel.java:1425)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.os.Parcel.readException(Parcel.java:1379)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2530)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4460)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2002)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1101)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.content.ContentResolver.query(ContentResolver.java:356)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.content.ContentResolver.query(ContentResolver.java:315)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at com.example.phonelog.MainActivity.onCreate(MainActivity.java:32)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.Activity.performCreate(Activity.java:5104)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-19 21:25:52.819: E/AndroidRuntime(3280):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-19 21:25:52.819: E/AndroidRuntime(3280):     ... 11 more
4

2 回答 2

6

就像错误跟踪说的那样:

requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG

您还需要添加

<uses-permission android:name="android.permission.READ_CALL_LOG" />

或者

<uses-permission android:name="android.permission.WRITE_CALL_LOG" />

到您在 AndroidManifest 中的权限使用列表。

当然,如果你只是在阅读,你不应该同时设置 WRITE 权限 - READ 就足够了。

于 2013-01-25T02:42:11.880 回答
0

此错误说,“我无法读取和写入您收到的通话记录,我需要从 CallLogProvider 读取和写入通话记录的权限”,CallLog 提供程序包含有关拨打和接听电话的信息。

现在您应该向清单添加WRITE_CALL_LOG权限,如果您遇到这样的错误,

java.lang.SecurityException: Permission Denial: 从 ProcessRecord{42931860 15578:com.hirecraft.jugunoo.passenger/u0a10095} (pid=15578, uid=10095) 打开提供程序 com.android.providers.contacts.CallLogProvider 需要 android.permission。 READ_CALL_LOG 或 android.permission.WRITE_CALL_LOG

<uses-permission android:name="android.permission.WRITE_CALL_LOG" />

否则,您应该在清单中添加READ_CALL_LOG权限,如果您遇到这样的错误,

java.lang.SecurityException:权限拒绝:从 pid=16013、uid=10095 读取 com.android.providers.contacts.CallLogProvider uri content://call_log/calls 需要 android.permission.READ_CALL_LOG 或 grantUriPermission()

<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
于 2014-09-27T16:03:25.813 回答