Android请求权限仅请求第一个

时间:2017-07-27 14:14:02

标签: android android-permissions

我在Android应用中工作,并且已在我的// language is typically "en", "fr" and so on var culture = new CultureInfo(language); CultureInfo.DefaultThreadCurrentCulture = culture; CultureInfo.DefaultThreadCurrentUICulture = culture; // You'll need to restart the app if you do this after application init 中声明了方法requestAppPermissions

嗯,这个代码发生了什么,出于某种原因,它要求第一个(在这种情况下,位置)并跳过其他代码;但是,我将一些其他权限移到第一个Activity,该方法正在请求第一个,跳过其他人。

我的代码出了什么问题?

if

1 个答案:

答案 0 :(得分:0)

类似的东西可能会有所帮助......

String[] neededPermissions = new String[]{
    android.Manifest.permission.ACCESS_FINE_LOCATION,
    android.Manifest.permission.CAMERA,
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE //if you can write, you can read
}

public static List<String> getPermissionsRequest(@NonNull Context context, 
                                                 @NonNull String[] permissions){
    List<String> permissionsToRequest = new ArrayList<>();
    for (int i = 0; i < permissions.length; i++){
        if (ContextCompat.checkSelfPermission(context, permissions[i]) 
                  != PackageManager.PERMISSION_GRANTED){
            permissionsToRequest.add(permissions[i]);
        }
    }
    return permissionsToRequest;
}

//now in the onCreate of your Activity you can do something like this:
List<String> permissionsToRequest = getPermissionsRequest(this, neededPermissions);
//if permissionsToRequest size is > 0 you need some more permission(s)
if (permissionsToRequest.size() > 0){
    ActivityCompat.requestPermissions(MainActivity.this,
         permissionsToRequest.toArray(new String[0]), PERMISSION_ALL_REQUEST_CODE);
}

//now in the onRequestPermissionsResult you can handle everything
//as always and proceeed if everything is ok

我目前尚未对此进行测试,但应该正常工作