棉花糖!如何以编程方式启用应用程序权限

时间:2016-05-16 07:57:16

标签: android permissions android-6.0-marshmallow

如何在Marshmallow中以编程方式打开相机,联系人,位置和存储等的应用程序权限。

我有代码,但它是打开的对话框,按钮'Deny'& '允许',我想直接将应用权限转为操作权,无需对话。

通过对话框打开'打开'权限的代码,而不是直接..

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_welcome);

   requestPermission();

}


private void requestPermission(){
    if (ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
        Toast.makeText(WelcomeActivity.this,"1. GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
    } else {
        Toast.makeText(WelcomeActivity.this,"2. GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(WelcomeActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(WelcomeActivity.this, "Permission Granted, Now you can access location data.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(WelcomeActivity.this, "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();
            }
        break;
    }
}

2 个答案:

答案 0 :(得分:0)

你想做什么是不可能的。 Marshmallow的权限系统的整个想法是让用户能够决定是否要授予权限。计划您的应用,以便在他们不授予时做出相应的反应。

答案 1 :(得分:0)

您的用户应该可以选择“不要再问我”,可以根据要求授予自动权限。你需要在你的应用程序中添加一个令人信服的案例,然后在获得权限失败时捕获