简体   繁体   English

发生Android java.lang.SecurityException

[英]Android java.lang.SecurityException occured

I am new in Android. 我是Android新手。 And I am Working On Dialer Through Launch Application. 我正在通过启动应用程序研究Dialer。 And I hide my Application Icon From the Menu. 我从菜单中隐藏了我的应用程序图标。 I successfully open the Application from Dialer In jellybean And Kitkat Device but when i open Application in Lollipop and Marshmallow System getting me Exception like, 我成功从Dellybean和Kitkat Device中的Dialer中打开了应用程序,但是当我在Lollipop和棉花糖系统中打开应用程序时,出现了如下异常:

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)

And then Application automatic close. 然后应用程序自动关闭。

The Android 6.0 (Marshmallow) introduced a new way to deal with permissions on Android. Android 6.0(棉花糖)引入了一种处理Android权限的新方法。 Read more about it here 在这里了解更多

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. 从Android 6.0(API级别23)开始,用户在应用程序运行时(而不是在安装应用程序时)授予应用程序权限。 This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. 这种方法简化了应用程序的安装过程,因为用户在安装或更新应用程序时无需授予权限。 It also gives the user more control over the app's functionality; 它还使用户可以更好地控制应用程序的功能。 for example, a user could choose to give a camera app access to the camera but not to the device location. 例如,用户可以选择授予摄像头应用访问摄像头的权限,但不授予设备位置的访问权限。 The user can revoke the permissions at any time, by going to the app's Settings screen. 通过转到应用程序的“设置”屏幕,用户可以随时撤消权限。

You need to declare your permissions at the manifest. 您需要在清单中声明您的权限。 Check for the current state: 检查当前状态:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

And request them if you need: 如果需要,请要求他们:

// 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.
    }
}

To deal with runtime permission in Android I usually use this library . 为了处理Android中的运行时权限,我通常使用此库 It really helps and reduce the code boilerplate. 它确实有助于减少代码样板。

From Android 6.0 we need to check if the permission is already granted or not, so in order to read contacts you need to first check if read_contacts permission is already granted or not. 从Android 6.0开始,我们需要检查权限是否已被授予,因此要读取联系人,您需要首先检查read_contacts权限是否已被授予。

Try : 尝试:

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.CAMERA}, 101);
        return;
    } else {
        // permission already granted
    }

and Listener after granting the permission 和侦听器授予权限后

    @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);
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM