Android权限拒绝 - 应用程序在FutureTask上崩溃

时间:2017-04-26 07:45:42

标签: android android-permissions

我有一个应用程序,需要请求允许从Android设备读取联系人。我知道我需要检查是否允许......

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
     requestPermissions(new String[]{READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
     loadContacts();
}

我的问题是,如果用户拒绝访问,我的应用就会崩溃

FATAL EXCEPTION: AsyncTask #2
Process: xxxxxxxxx, PID: 10376
java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:309)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)
      Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{3ad4917 10376:xxxxxxxxx/u0a196} (pid=10376, uid=10196) 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:4241)
      at android.app.ActivityThread.acquireProvider(ActivityThread.java:6392)
      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2321)
      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1521)
      at android.content.ContentResolver.query(ContentResolver.java:486)
      at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
      at android.content.CursorLoader.loadInBackground(CursorLoader.java:42)
      at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
      at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
      at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
      at android.os.AsyncTask$2.call(AsyncTask.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 

我知道我没有访问权限,但应用程序肯定不会崩溃,这只是意味着我无法阅读联系人?它似乎在谈论一个FutureTask,我不确定这是什么?我已经尝试评论实际进行联系人访问的所有代码,它仍然会在尝试阅读联系人之前崩溃。我只是想优雅地处理这种情况而且我似乎没有机会?

Manifest具有权限集

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

此外,如果我进入应用程序管理器并更改应用程序的设置,以便应用程序可以访问联系人,应用程序运行正常,我还没有访问联系人,这纯粹是在启动时

2 个答案:

答案 0 :(得分:1)

请使用以下代码检查权限

  

首先在你的oncreate方法中调用isStoragePermissionGranted1(),然后再调用loadContacts()

喜欢

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(isStoragePermissionGranted1())
      {
             loadContacts();
      }
}


  public boolean isStoragePermissionGranted1() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
                == PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.WRITE_CONTACTS)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG", "Permission is granted");
            return true;
        } else {
            Log.v("TAG", "Permission is revoked");
            ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{android.Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}, 1);
            return false;
        }
    } else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG", "Permission is granted");
        return true;
    }
}
  

要检查权限被拒绝或授予,我们可以使用

@Override
    public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
        switch (requestCode) {
         case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! do the
            //  task you need to do.

        } else {

            // permission denied, boo! Disable the
            // functionality that depends on this permission.
        }
        return;
    }

    // other 'switch' lines to check for other
    // permissions this app might request
}

}

  

希望这会有所帮助

答案 1 :(得分:0)

我建议你使用ActivityCompat,它更好地支持它

    if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_CONTACTS) != PermissionChecker.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
    } else {
        loadContacts();
    }