java - Android java.lang.SecurityException 发生

标签 java android

我是 Android 新手。我正在通过启动应用程序开发拨号器。我从菜单中隐藏了我的应用程序图标。我成功地从 Dialer In jellybean 和 Kitkat Device 打开了应用程序,但是当我在 Lollipop 和 Marshmallow System 中打开应用程序时,出现了异常,

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)

然后应用程序自动关闭。

最佳答案

Android 6.0 (Marshmallow) 引入了一种处理 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. 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.

您需要在 list 中声明您的权限。 检查当前状态:

// 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 .它确实有助于减少代码样板。

关于java - Android java.lang.SecurityException 发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699073/

相关文章:

android - 为什么.apk文件没有在真实设备上运行

java - 从 MySQL 获取数据到 Android 应用程序

java - 执行ant clean,构建失败,java版本错误

java - 通过反射获取方法返回类型

android - 蜂窝 Activity 自发地重新启动

android - 关于Flutter中的Gradle和Android平台

java - 多线程中面临的问题

java - java中数组的while循环问题

java - 使用 ModiTect 生成模块信息并创建运行时镜像

java - 安卓 Java : Activity called AFTER onPause( )