Android棉花糖权限? [英] Android Marshmallow permissions?

查看:157
本文介绍了Android棉花糖权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户可以接受访问权限的地方显示权限提示,就像我想访问用户联系人一样,所以我想显示带有两个选项allow的提示,并拒绝任何示例和源代码。

I want to show permission prompt where user can accept the access of that permission,Like i want to access users contacts so i want to show prompt with two option allow and deny any example and source code.

推荐答案

这样做

private void PerrmissionWork() {

    List<String> permissionsNeeded = new ArrayList<String>();

    final List<String> permissionsList = new ArrayList<String>();
    if (!addPermission(permissionsList,
            Manifest.permission.ACCESS_FINE_LOCATION))
        permissionsNeeded.add("GPS");
    if (!addPermission(permissionsList,
            Manifest.permission.ACCESS_COARSE_LOCATION))
        permissionsNeeded.add("GPS COARSE");


    if (permissionsList.size() > 0) {
        if (permissionsNeeded.size() > 0) {
            // Need Rationale
            String message = "You need to grant access to "
                    + permissionsNeeded.get(0);
            for (int i = 1; i < permissionsNeeded.size(); i++)
                message = message + ", " + permissionsNeeded.get(i);
            showMessageOKCancel(message,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            requestPermissions(permissionsList
                                    .toArray(new String[permissionsList
                                            .size()]),
                                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                        }
                    });
            return;
        }
        requestPermissions(
                permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        return;
    }
    splashMainWork();
}

// mapWork();

private boolean addPermission(List<String> permissionsList,
        String permission) {
    if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
        permissionsList.add(permission);
        // Check for Rationale Option
        if (!shouldShowRequestPermissionRationale(permission))
            return false;
    }
    return true;
}

private void showMessageOKCancel(String message,
        android.content.DialogInterface.OnClickListener onClickListener) {
    new AlertDialog.Builder(context).setMessage(message)
            .setPositiveButton("OK", onClickListener).setCancelable(false)
            .setNegativeButton("Cancel", null).create().show();

}

@Override
public void onRequestPermissionsResult(int requestCode,
        String[] permissions, int[] grantResults) {
    switch (requestCode) {
    case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {
        Map<String, Integer> perms = new HashMap<String, Integer>();
        // Initial
        perms.put(Manifest.permission.ACCESS_FINE_LOCATION,
                PackageManager.PERMISSION_GRANTED);
        perms.put(Manifest.permission.ACCESS_COARSE_LOCATION,
                PackageManager.PERMISSION_GRANTED);

        // Fill with results
        for (int i = 0; i < permissions.length; i++)
            perms.put(permissions[i], grantResults[i]);
        // Check for ACCESS_FINE_LOCATION
        if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
                ) {
            // All Permissions Granted
            splashMainWork();
        } else {
            // Permission Denied
            Toast.makeText(context, "Some Permission is Denied",
                    Toast.LENGTH_SHORT).show();
        }
    }
        break;
    default:
        super.onRequestPermissionsResult(requestCode, permissions,
                grantResults);
    }
}

并在创建时调用此方法。

and call this method in on create like this..

    if (Build.VERSION.SDK_INT >= 23) {
        PerrmissionWork();
    } else {
       splashMainWork();
    }

我正在启动工作方法中的位置并使用位置权限,您可以使用联系人权限和代替飞溅工作做您的联系代码..并且还有很多演示可用,请尝试用google进行尝试,他们可以为您提供很好的解释..在开发人员方面也有很好的解释。

< a href = http://developer.android.com/training/permissions/requesting.html rel = nofollow>此处 .. 尝试此博客非常有帮助

i am getting location in splash work method and using location permission you can use contact permisssion and in place of splash work do your contact code.. and there are lots of demo also available try google they can give you good explanations.. and there is very good explanation on developer sit too.
here ..Try this blog very helpful

这篇关于Android棉花糖权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆