【问题标题】:Android app crashes when trying to read contacts in Android Marshmallow (Android M)尝试在 Android Marshmallow (Android M) 中读取联系人时,Android 应用程序崩溃
【发布时间】:2015-11-13 22:37:03
【问题描述】:

我已将我的应用程序定位到 SDK 版本 23 (Android Marsmallow)

当我尝试从我的应用程序中读取联系人时,我得到了这个异常,尽管我在我的 Android 清单文件中授予了 READ_CONTACTS 权限。 我想简单的 try catch 不会是正确的处理方式。

异常详情:

原因:java.lang.SecurityException: Permission Denial: 从 pid=8373 读取 com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data,uid=10152 需要 android.permission。 READ_CONTACTS,或 grantUriPermission()

我的清单权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【问题讨论】:

  • 粘贴你的清单文件
  • @Neha 更新了我的清单权限。
  • 我需要你的完整清单文件..希望你没有在应用程序标签内声明权限
  • 请检查您的清单文件顶部是否有 并尝试添加写入外部存储的权限。希望它有效:)

标签: android user-permissions


【解决方案1】:

运行时权限

如果您需要添加的权限未列在正常权限下,则需要处理“运行时权限”。运行时权限是在应用程序运行时根据需要请求的权限。这些权限将向用户显示一个对话框,类似于以下:

添加“运行时权限”的第一步是将其添加到 AndroidManifest:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codepath.androidpermissionsdemo" >

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

您需要发起权限请求并处理结果。以下代码显示了如何在 Activity 的上下文中执行此操作,但也可以在 Fragment 中执行此操作。

    // MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // In an actual app, you'd want to request a permission when the user performs an action
        // that requires that permission.
        getPermissionToReadUserContacts();
    }

    // Identifier for the permission request
    private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;

    // Called when the user is performing an action which requires the app to read the
    // user's contacts
    public void getPermissionToReadUserContacts() {
        // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
        // checking the build version since Context.checkSelfPermission(...) is only available
        // in Marshmallow
        // 2) Always check for permission (even if permission has already been granted)
        // since the user can revoke permissions at any time through Settings
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            // The permission is NOT already granted.
            // Check if the user has been asked about this permission already and denied
            // it. If so, we want to give more explanation about why the permission is needed.
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_CONTACTS)) {
                // Show our own UI to explain to the user why we need to read the contacts
                // before actually requesting the permission and showing the default UI
            }

            // Fire off an async request to actually get the permission
            // This will show the standard permission request dialog UI
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                    READ_CONTACTS_PERMISSIONS_REQUEST);
        }
    }

    // Callback with the request from calling requestPermissions(...)
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        // Make sure it's our original READ_CONTACTS request
        if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
            if (grantResults.length == 1 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

这里是原帖

https://guides.codepath.com/android/Understanding-App-Permissions

【讨论】:

  • 如果您添加来自codepath 的原始网址会很有帮助。
  • @chancyWu 添加了原始网址。
【解决方案2】:

这是我想要的。

http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

完整的权限相关解决方案。希望它会帮助某人。

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多