2016-10-01 178 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 
}