2016-10-01 175 views
1

我正在嘗試請求我的啓動程序活動權限。對於API < 23,它完美。但是,當我在運行API 23的設備上測試應用程序時,它會顯示:「PostPaid Balance已停止。」我點擊「關閉應用程序按鈕」,應用程序關閉,並立即要求一個許可。我接受了。然後我點擊應用程序圖標重新打開,同樣的事情發生,除了現在它要求下一個權限。然後點擊應用程序圖標,這一次正確執行。 它似乎是一次要求一個權限。有關如何去做這件事的任何想法?Android API 23請求多個權限

// Below code is implemented on onCreate() of the launcher activity. 
if (Build.VERSION.SDK_INT < 23) { 
     ActivityCompat.checkSelfPermission(this.getApplicationContext(), "android.permission.READ_SMS"); 
     ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_CALL_LOG); 
     ActivityCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.READ_PHONE_STATE); 

     if ((ActivityCompat.checkSelfPermission(this, "android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED)) { 
      requestSmsPermission(); 
     } 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 
      requestPhoneStatePermission(); 
     } 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) { 
      requestCallLogPermission(); 
     } 
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if ((this.checkSelfPermission("android.permission.READ_SMS") != PackageManager.PERMISSION_GRANTED) && 
       (this.checkSelfPermission(Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) && 
       (this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)) { 
      this.requestPermissions(new String[]{"android.permission.READ_SMS", Manifest.permission_group.PHONE}, REQUEST_SMS); 

     } 
    } 

enter image description here

+1

請求是在單個'requestPermissions所有三種權限()'調用。此外,還不清楚爲什麼您的代碼基於API級別設置爲執行不同的操作。 'ActivityCompat'和'ContextCompat'是向後兼容的。 – CommonsWare

+0

我設置的代碼做不同的事情,因爲某些功能不可用於API <23 –

+0

您會推薦什麼?如果API是23並且不理會if(API <23),我應該只要求權限? –

回答

2

要回答你如何在一氣呵成的多個權限要求的問題,將權限添加到您的字符串數組。在這個例子中,我想請求權限READ_PHONE_STATE和WRITE_EXTERNAL_STORAGE:

ArrayList<String> arrPerm = new ArrayList<>(); 
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { 
    arrPerm.add(Manifest.permission.READ_PHONE_STATE); 
} 
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
    arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); 
} 
if(!arrPerm.isEmpty()) { 
    String[] permissions = new String[arrPerm.size()]; 
    permissions = arrPerm.toArray(permissions); 
    ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST); 
} 

現在,檢查被授予哪些權限:

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

    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0) { 
       for(int i = 0; i < grantResults.length; i++) { 
        String permission = permissions[i]; 
        if(Manifest.permission.READ_PHONE_STATE.equals(permission)) { 
         if(grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
          // you now have permission 
         } 
        } 
        if(Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) { 
         if(grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
          // you now have permission 
         } 
        } 
       } 
      } else { 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      break; 
     } 
    } 

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