Android Marshmallow权限对话框显示错误后

时间:2016-12-24 07:03:36

标签: android permissions android-6.0-marshmallow

我正在编写一个Android应用程序,需要对联系人的权限。 Fow Android 6我在MainActivity onCreate方法中写道:

if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.READ_CONTACTS)) {
            } else {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            }
        }

安装应用程序时,首先出现错误对话框:不幸的是,应用程序已停止

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.narehh.contactlist, PID: 17813
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.narehh.contactlist/com.example.narehh.contactlist.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{554b48c 17813:com.example.narehh.contactlist/u0a496} (pid=17813, uid=10496) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                      at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:7331)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{554b48c 17813:com.example.narehh.contactlist/u0a496} (pid=17813, uid=10496) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                      at android.os.Parcel.readException(Parcel.java:1621)
                      at android.os.Parcel.readException(Parcel.java:1574)
                      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4292)
                      at android.app.ActivityThread.acquireProvider(ActivityThread.java:6499)
                      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2407)
                      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1521)
                      at android.content.ContentResolver.query(ContentResolver.java:486)
                      at android.content.ContentResolver.query(ContentResolver.java:445)
                      at com.example.narehh.contactlist.MainActivity.getContacts(MainActivity.java:96)
                      at com.example.narehh.contactlist.MainActivity.onCreate(MainActivity.java:84)
                      at android.app.Activity.performCreate(Activity.java:6904)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                      at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:7331) 
                      at java.lang.reflect.Method.invoke(Native Method)

仅显示权限对话框后:

  

允许联系人列表访问您的联系人。

代码有什么问题?

0 个答案:

没有答案