Marshmallow permission

2019-07-23 18:01发布

I'm trying to check permission in activity as below,

int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNT);

But I'm getting error at GET_ACCOUNT as Cannot resolve symbol 'GET_ACCOUNT'. Kindly help what code should I add to fix this?

Thanks in advance.

3条回答
一夜七次
2楼-- · 2019-07-23 18:34

The following code worked for me,

Declare below code as:

public static final int PERMISSIONS_REQUEST_GET_ACCOUNT = 133; // any number

Call following code in your onCreate method:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if(ContextCompat.checkSelfPermission(Activity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(SplashActivity.this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.CAMERA,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.GET_ACCOUNTS},
                    PERMISSIONS_REQUEST_GET_ACCOUNT);
        }else{
            //furtherCode
        }
    }else{
        //furtherCode
    }

in method onRequestPermissionsResult method,

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch(requestCode){
        case PERMISSIONS_REQUEST_GET_ACCOUNT :
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "Granted", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "No such permission to access account!", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}
查看更多
做自己的国王
3楼-- · 2019-07-23 18:35

First GET_ACCOUNT is not defined in android.Manifest.permission, it is GET_ACCOUNTS

Second, change your Manifest file to use GET_ACCOUNTS

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

then in your code file i.e. java file write like this.

ContextCompat.checkSelfPermission(this, android.Manifest.permission.GET_ACCOUNTS);

Check documentation GET_ACCOUTNS.

查看更多
放我归山
4楼-- · 2019-07-23 18:36

It should be

Manifest.permission.GET_ACCOUNTS

instead of

Manifest.permission.GET_ACCOUNT  

Try like this

int permission = ContextCompat.checkSelfPermission(this,Manifest.permission.GET_ACCOUNTS);

Don't forget to add permission on your Manifest

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

Import this library in your class

import android.Manifest;
查看更多
登录 后发表回答