启动时的运行时权限

时间:2016-07-13 21:11:04

标签: android android-6.0-marshmallow runtime-permissions

我有一个需要CAMERA和WRITE_SETTINGS权限的手电筒,我需要先处理这些我已经知道如何操作的权限,然后如果它被授予,例如,启动我的主要活动...... (对不起,我是android新手)

2 个答案:

答案 0 :(得分:6)

从您的gradle文件中的github添加此库

比添加此代码

PermissionListener permissionlistener = new PermissionListener() {
    @Override
    public void onPermissionGranted() {
        //The user have conceded permission
        Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionDenied(ArrayList<String> deniedPermissions) {
        //close the app or do whatever you want
        Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
    }
};

new TedPermission(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.check();

或者,如果您更喜欢本地化,请查看Google docs

答案 1 :(得分:0)

在清单文件上添加权限:

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

在您的主要活动Java文件“ onCreate ”中,添加以下代码

    String[] PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CALL_PHONE};
    if (
                    (ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED) ||
                    (ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[1]) != PackageManager.PERMISSION_GRANTED)
            )
    {
        ActivityCompat.requestPermissions(MapsActivity.this, PERMISSIONS, REQUEST_INTERNET);
        finish();
    }