2016-07-24 12 views
-1

全部如何引導用戶在我的應用程序的系統設置中打開權限

我有一個關於android 6權限的問題。如果用戶拒絕授予權限,則用戶無法在我的應用中使用某些功能。我如何使用代碼來指導用戶在系統設置應用程序中的應用程序權限設置,以便用戶可以打開此權限。我沒有找到相關的方法。你能幫我解答嗎? 謝謝

回答

0

在棉花糖你應該請求權限。 此鏈接可以幫助你。

https://stackoverflow.com/a/33666161/5577853

 @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     Log.d("", "Permission callback called-------"); 
     switch (requestCode) { 
      case REQUEST_ID_MULTIPLE_PERMISSIONS: { 

       Map<String, Integer> perms = new HashMap<String, Integer>(); 
       // Initialize the map with both permissions 

       perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED); 
       perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED); 
       // Fill with actual results from user 
       if (grantResults.length > 0) { 
        for (int i = 0; i < permissions.length; i++) 
         perms.put(permissions[i], grantResults[i]); 
        // Check for both permissions 
        if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
          && perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { 
         Log.d("", " permission granted"); 
         // process the normal flow 
         //else any one or both the permissions are not granted 
        } else { 
          Log.d("", "Some permissions are not granted ask again "); 
          //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission 
    // shouldShowRequestPermissionRationale will return true 
          //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup. 
          if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) 
            || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { 
           showDialogOK("This app needs something something, please grant permissions", 
             new DialogInterface.OnClickListener() { 
              @Override 
              public void onClick(DialogInterface dialog, int which) { 
               switch (which) { 
                case DialogInterface.BUTTON_POSITIVE: 
                 checkAndRequestPermissions(); 
                 break; 
                case DialogInterface.BUTTON_NEGATIVE: 
                 // proceed with logic by disabling the related features or quit the app. 
                 Toast.makeText(getApplicationContext(), "Please go to settings and enable permissions.", 2000).show(); 
                 break; 
               } 
              } 
             }); 
          } 
          //permission is denied (and never ask again is checked) 
            //shouldShowRequestPermissionRationale will return false 
          else { 
           Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG) 
             .show(); 
    //       //proceed with logic by disabling the related features or quit the app. 
          } 
        } 
       } 
      } 
     } 

    } 

    private void showDialogOK(String message, DialogInterface.OnClickListener okListener) { 
     new AlertDialog.Builder(this) 
       .setMessage(message) 
       .setPositiveButton("OK", okListener) 
       .setNegativeButton("Cancel", okListener) 
       .create() 
       .show(); 
    } 

    private boolean checkAndRequestPermissions() { 

     int permissionWriteExternal = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
     int permissionCallPhone = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE); 

     List<String> listPermissionsNeeded = new ArrayList<String>(); 

     if (locationPermission != PackageManager.PERMISSION_GRANTED) { 
      listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); 
     } 

     } 
     if (permissionCallPhone != PackageManager.PERMISSION_GRANTED) { 
      listPermissionsNeeded.add(Manifest.permission.CALL_PHONE); 
     } 
     if (!listPermissionsNeeded.isEmpty()) { 
      ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS); 
      return false; 
     } 
     return true; 
    } 
+0

您可以再次請求獲取所需權限。 –

+0

如果用戶拒絕並選擇永不問,我根本無法獲得權限對話框。所以我想引導用戶自己設置並打開它。我認爲最好只通知用戶他沒有權限。 – mmm2006

+0

@ mmm2006嘗試以上代碼 –

0

試試下面的代碼。

public static void mOpenActivity(Context context) { 
    if (context == null) { 
     return; 
    } 
    final 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); 
} 
+0

@ mmm2006檢查這個是否工作 –

+0

謝謝。我會嘗試。 – mmm2006

相關問題