2016-10-04 41 views

回答

6

如果用戶選擇「永不再提問」,則無法打開請求權限對話框。 但您可以將信息顯示給用戶。

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case REQUEST_CODE: 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show(); 
        // do your work here 
       } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) { 
        Toast.makeText(MainActivity.this, "Go to Settings and Grant the permission to use this feature.", Toast.LENGTH_SHORT).show(); 
        // User selected the Never Ask Again Option 
       } else { 
        Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show(); 
       } 
       break; 
     } 
    } 

設置屏幕: 要打開設置界面,您可以使用下面的意圖。

Intent i = new Intent(); 
    i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
    i.addCategory(Intent.CATEGORY_DEFAULT); 
    i.setData(Uri.parse("package:" + context.getPackageName())); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
    context.startActivity(i); 

欲瞭解更多信息,您可以檢查線程How to programmatically open the Permission Screen for a specific app on Android Marshmallow?

+0

我可以將用戶重定向到Settings頁面嗎? –

+0

檢查更新的答案。 –

+0

非常感謝Deepak –

相關問題