Android N权限问题

时间:2016-07-27 09:01:05

标签: android android-permissions android-7.0-nougat

我的测试设备上有Android N Developer预览版,我在应用启动期间要求获得运行时权限。

但不幸的是,应用程序没有显示警告对话框来授予权限。我使用以下代码:

启动器活动

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if(checkPermissions()) {
            renderView();
        }
    }

    private boolean checkPermissions() {

        int permissionWriteStorage = ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE);
        int permissionPhoneState = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        Log.i("permission",(permissionWriteStorage == PackageManager.PERMISSION_GRANTED) + "");
        //This prints false

        List<String> listPermissionsNeeded = new ArrayList<>();
        if (permissionWriteStorage != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission_group.STORAGE);
        }
        if (permissionPhoneState != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
        }

        if (!listPermissionsNeeded.isEmpty()) {
            Log.i("permission", "in if");
            //I get this in the logs

            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }
}

的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId 'com.***.***'
        minSdkVersion 16
        targetSdkVersion 24
    }
}

编辑:解决方案(1/2)

将构建工具版本更改为24.0.1我收到了READ_PHONE_STATE权限的提醒,但未获得权限组STORAGE的提醒。 我们是否需要单独询问权限组的每个权限?

编辑2:解决方案(2/2)

我设法通过将Manifest.permission_group.STORAGE替换为Manifest.permission.WRITE_EXTERNAL_STORAGE来解决此问题。根据文档,您只需要确认整个组的一个危险许可。

  

用户只需为每个权限组授予一次权限。如果您的应用请求该组中的任何其他权限(在您的应用清单中列出),系统会自动授予它们。

5 个答案:

答案 0 :(得分:1)

我不确定但是更改了构建工具版本

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
    applicationId 'com.***.***'
    minSdkVersion 16
    targetSdkVersion 24
}

答案 1 :(得分:0)

用户ActivityCompat代替ContextCompat

int permissionWriteStorage = ActivityCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE);
int permissionPhoneState = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

答案 2 :(得分:0)

我知道这种技术很乱,但为什么不简单地使用呢?

您也可以获得多种权限。

void getPermission(){
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission_group.STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            ActivityCompat.requestPermissions(Final_maps.this,
                    new String[]{android.Manifest.permission_group.STORAGE, android.Manifest.permission.READ_PHONE_STATE},
                    CODE);

    }

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case CODE:
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setLocationEnabled();
                Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission Denied!", Toast.LENGTH_SHORT).show();
                finish();
            }
    }
}

答案 3 :(得分:0)

如果您未实施onRequestPermissionsResult方法,则不会显示权限提醒。

答案 4 :(得分:0)

在清单中授予权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  

然后将代码粘贴到“活动”中

 private void getPermission() {

    if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED) {
        if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
        }
    }
}
  

也调用此替代方法

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == PERMISSION_REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {


        }
    }

}