2017-04-01 76 views

回答

1

您可以在初始屏幕或第一屏幕上處理權限。

如果多個權限然後處理所有的一次。

如果用戶拒絕任何權限,則顯示消息需要一些權限。

希望它能幫助你。

以下是如何處理多個權限的示例。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 


if(checkAndRequestPermissions()){ 
    //all permission granted 
}else{ 
    //require all permission. 
} 

} 


private boolean checkAndRequestPermissions() { 
    int camerapermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA); 
    int phonestate = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE); 
    int location = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); 
    int recordaudio = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO); 
    int permissionLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 


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

    if (camerapermission != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.CAMERA); 
    } 
    if (phonestate != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE); 
    } 
    if (location != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); 
    } 
    if (recordaudio != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO); 
    } 

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



@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 

    switch (requestCode) { 
     case REQUEST_ID_MULTIPLE_PERMISSIONS: { 

      Map<String, Integer> perms = new HashMap<>(); 

      perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED); 
      perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED); 
      perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED); 
      perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED); 
      perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, 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.CAMERA) == PackageManager.PERMISSION_GRANTED 
         && perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED 
         && perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
         && perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED 
         && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED 
         ) { 
        Log.d(TAG, "camera & location services permission granted"); 

        // here you can do your logic all Permission Success Call 
         moveToNxtScreen(); 

       } else { 
        Log.d(TAG, "Some permissions are not granted ask again "); 
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
         showDialogOK("Some Permissions are required for Open Camera", 
           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. 
               dialog.dismiss(); 
               break; 
             } 
            } 
           }); 
        } else { 
         explain("You need to give some mandatory permissions to continue. Do you want to go to app settings?"); 
        } 
       } 
      } 
     } 
    } 

} 

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

private void explain(String msg) { 
    final android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(this); 
    dialog.setMessage(msg) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        Utils.startInstalledAppDetailsActivity(LoginActivity.this); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface paramDialogInterface, int paramInt) { 
        dialog.create().dismiss(); 
        finish(); 
       } 
      }); 
    dialog.show(); 
} 
+0

Thanx Sachin,但如果用戶拒絕設置後啓動畫面的權限?所以我已經到處檢查。 –

+0

然後你需要檢查每次授予權限或不授權。如果權限被拒絕,則需要簡單的顯示烤麪包權限和開放權限設定畫面。檢查如何打開權限屏幕http://stackoverflow.com/a/32983128/3416642 – Sach

1

創建兩個變量,如

private static final int REQUEST_CODE_PERMISSION = 1; 
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION; 

後創建方法類似這樣的

private void getPermission() { 
    if(Build.VERSION.SDK_INT>= 23) { 

     if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(MainActivity.this, 
        new String[]{mPermission, 
        }, 
        REQUEST_CODE_PERMISSION); 
      return; 
     } 

     else 
     { 
      /* your code if permission already access 
     } 
    } 
} 

覆蓋後onRequestPermissionsResult這樣

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    Log.e("Req Code", "" + requestCode); 
    if (requestCode == REQUEST_CODE_PERMISSION) { 
     if (grantResults.length == 1 && 
       grantResults[0] == MockPackageManager.PERMISSION_GRANTED) { 
      methodRedirect(); 
     } 
     else{ 
      this.getPermission(); 
     } 
    } 
} 

這樣即使用戶拒絕權限的方法,它會再次顯示。

您可以像這樣管理多個權限。