我是Android新手。我正在通过启动应用程序研究拨号器。我从菜单中隐藏了我的应用程序图标。我成功地从Dialer在jellybean和Kitkat Device中打开应用程序,但是当我在Lollipop和Marshmallow系统中打开应用程序时,让我感觉异常,
E/GooglePlusContactsSync: Failed to clear out contacts
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{ff7ceee 26321:com.google.android.apps.plus/u0a89} (pid=26321, uid=10089) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3594)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4799)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1466)
at android.content.ContentResolver.query(ContentResolver.java:475)
at android.content.ContentResolver.query(ContentResolver.java:434)
at dit.a(PG:440)
at dit.b(PG:1388)
at diu.run(PG:325)
at java.lang.Thread.run(Thread.java:818)
E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/fs_id from pid=2208, uid=10089 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:453)
然后应用程序自动关闭。
答案 0 :(得分:2)
Android 6.0(Marshmallow)推出了一种处理Android权限的新方法。 Read more about it here
从Android 6.0(API级别23)开始,用户授予权限 应用程序运行时的应用程序,而非安装应用程序时的应用程序。这个 方法简化了应用安装过程,因为用户没有 需要在安装或更新应用程序时授予权限。它也是 让用户更好地控制应用程序的功能;例如, 用户可以选择让相机应用程序访问相机,但不能 到设备位置。用户可以撤消任何权限 时间,转到应用程序的“设置”屏幕。
您需要在清单中声明您的权限。 检查当前状态:
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
如果您需要,请提出要求:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
要处理Android中的运行时权限,我通常使用this library。它确实有助于减少代码样板。
答案 1 :(得分:0)
在Android 6.0中,我们需要检查是否已授予权限,因此,为了阅读联系人,您需要首先检查是否已授予read_contacts权限。
尝试:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 101);
return;
} else {
// permission already granted
}
授予权限后和监听器
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission granted
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}