Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓棉花糖权限?_Android_Android Permissions_Android 6.0 Marshmallow_Android Runtime - Fatal编程技术网

Android 安卓棉花糖权限?

Android 安卓棉花糖权限?,android,android-permissions,android-6.0-marshmallow,android-runtime,Android,Android Permissions,Android 6.0 Marshmallow,Android Runtime,我想显示权限提示,用户可以在其中接受该权限的访问,就像我想访问用户联系人一样,所以我想显示带有两个选项的提示:允许和拒绝任何示例和源代码。这样做吗 private void PerrmissionWork() { List<String> permissionsNeeded = new ArrayList<String>(); final List<String> permissionsList = new ArrayList<Str

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

这样做吗

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);
    }
}
我在splash work方法中获取位置,使用位置权限您可以使用联系权限,并在splash work中使用您的联系代码。。还有很多演示,你可以试试谷歌,他们可以给你很好的解释。。开发者坐姿也有很好的解释。
..

查看有关请求权限的官方文档。下面是使用该文档的示例类:

public class PermissionCheck {


    private void requestIfNeeded() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(thisActivity,
                        Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Manifest.permission.READ_CONTACTS)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    handleSuccessfulPermissionsRequest();

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    handlePermissionDenied();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    private void handleSuccessfulPermissionsRequest() {
            Toast.makeText(this, "Request Successful!",Toast.LENGTH_SHORT).show();
    }

    private void handlePermissionDenied() {
        Toast.makeText(this, "Request Denied!",Toast.LENGTH_SHORT).show();
    }

}

谢谢你的回答,工作@夜怒。。我的荣幸。。
public class PermissionCheck {


    private void requestIfNeeded() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(thisActivity,
                        Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                    Manifest.permission.READ_CONTACTS)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{Manifest.permission.READ_CONTACTS},
                        MY_PERMISSIONS_REQUEST_READ_CONTACTS);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
            String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    handleSuccessfulPermissionsRequest();

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    handlePermissionDenied();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    private void handleSuccessfulPermissionsRequest() {
            Toast.makeText(this, "Request Successful!",Toast.LENGTH_SHORT).show();
    }

    private void handlePermissionDenied() {
        Toast.makeText(this, "Request Denied!",Toast.LENGTH_SHORT).show();
    }

}