简体   繁体   English

Android权限被拒绝-FutureTask应用崩溃

[英]Android Permission Denial - App crashes on FutureTask

I've got an app that needs to ask for permission to read contacts from an Android device. 我有一个需要征求许可才能从Android设备读取联系人的应用程序。 I'm aware of that I need to check for permission... 我知道我需要检查权限...

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

My problem is that if the user denies access my app is crashing 我的问题是,如果用户拒绝访问,我的应用程序将崩溃

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) 

I understand that I don't have access , but surely the app shouldn't crash, it should just mean I can't read the contacts? 我了解我没有访问权限 ,但可以肯定该应用程序不应崩溃,这仅意味着我无法阅读联系人? It seems to be talking about a FutureTask which I'm not sure what this is? 似乎是在谈论FutureTask ,我不确定这是什么? I've tried commenting out all the code that actually does the contact access and it still crashes before it gets anywhere near trying to read the contacts. 我尝试注释掉所有实际进行联系人访问的代码,但在尝试读取联系人之前,它仍然崩溃。 I just want to handle this situation gracefully and I don't appear to be getting the chance? 我只是想优雅地处理这种情况,而我似乎没有机会?

The Manifest has the permission set 清单已设置权限

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

Also if I go into Application Manager and change the settings for the app so that the app does have access to contacts the app runs fine, and I've still not accessed the contacts yet, this is purely on start up 另外,如果我进入“应用程序管理器”并更改应用程序的设置,以使该应用程序确实可以访问联系人,则该应用程序运行良好,而我仍未访问该联系人,这纯粹是在启动时

Please use following code to check permissions 请使用以下代码检查权限

call isStoragePermissionGranted1() first in your oncreate method after that the loadContacts() 在loadContacts()之后,首先在oncreate方法中调用isStoragePermissionGranted1()。

Like 喜欢

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

To check permissions is denied or granted we can use 要检查权限是否被拒绝或授予,我们可以使用

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

} }

Hope this will help 希望这会有所帮助

I suggest you use the ActivityCompat , which supports it better 我建议您使用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();
    }

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

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