我提出一個應用程序,它允許自動添加的權限爲Android 6.0和Android M.我的權限包括如何在Android 6中添加多個權限並在模擬器中測試?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
我用下面的代碼添加權限的Android 6.0版本
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
Log.d ("TAG", "Permissions are granted");
}
}
private boolean checkAndRequestPermissions() {
if (Build.VERSION.SDK_INT >= 23) {
int permissionSTORAGE = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionRECORDAUDIO = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionSTORAGE != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissionRECORDAUDIO != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
else
return true;
}}
我在上面的代碼中有兩個問題。
- 是否可以檢查模擬器(Android M,SDK 24)中的權限?
- 我的實現是否正確?我沒有真正的設備,所以我無法確認上面的代碼。
其確認的工作在這裏有看http://stackoverflow.com/questions/38141523/directory-creation-not-working-in-marshmallow-android/38141778#38141778 –
檢查本指南 - https:// inthecheesefactory。 com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en - 不確定模擬器是否正確,但我已在真實設備上進行了測試。應該同樣工作 – Tasos