如何在Marshmallow中的Android应用程序运行时询问权限

时间:2023-01-20 21:02:12

I have added all the permissions in the manifest file. In Lollipop and all the application will ask for permissions during install the app, but when the application is installed in marshmallow the permission is not asking.

我已在清单文件中添加了所有权限。在Lollipop中,所有应用程序都会在安装应用程序时请求权限,但是当应用程序安装在marshmallow中时,权限不会询问。

Manifest Permissions are

清单权限是

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />

3 个解决方案

#1


1  

May be want to use third party library to simplify permission process, these libraries can help to you.

可能要使用第三方库来简化权限过程,这些库可以对您有所帮助。

PermissionsDispatcher

PermissionsDispatcher

Dexter

德克斯特

RxPermissions

RxPermissions

#2


0  

First declare :

首先声明:

// Assume thisActivity is the current activity

    int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.WRITE_CALENDAR);

Request the permissions you need :

请求您需要的权限:

// 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.
    }
}

Handle the permissions request response:

处理权限请求响应:

@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.

            } else {

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

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

#3


0  

Firstly, you need to classify all these permissions. On Android M version, they are mainly divided to Normal permissions(Like ACCESS_NETWORK_STATE in your Manifest file) and Dangerous permissions(like ACCESS_FINE_LOCATION). For Normal ones you don't have to request them which are granted automatically without a user reminder on installation any more. What you only need to do is just adding them in Manifest!

首先,您需要对所有这些权限进行分类。在Android M版本上,它们主要分为普通权限(如清单文件中的ACCESS_NETWORK_STATE)和危险权限(如ACCESS_FINE_LOCATION)。对于普通的,您不必请求自动授予它们,而不再需要安装用户提醒。你只需要在Manifest中添加它们!

However, Dangerous permissions are more complicated to handle. On app starts up or runs to corresponding feature, you MUST firstly check whether the permissions have been acquired by Context.checkSelfPermission(String permission). Call Context.requestPermissions(String[] permissions, int requestCode) to request by system dialog and get result onRequestPermissionsResult().

但是,危险权限处理起来更复杂。在应用程序启动或运行到相应的功能时,您必须首先检查Context.checkSelfPermission(字符串权限)是否已获取权限。调用Context.requestPermissions(String [] permissions,int requestCode)以通过系统对话框请求并获取结果onRequestPermissionsResult()。

Good news is you can request a bunch of permissions by one API as app launches. Furthermore, Dangerous permissions are managed in several groups. For example, you only need to request for ACCESS_FINE_LOCATION and get ACCESS_COARSE_LOCATION permission as well.

好消息是,您可以通过一个API请求一堆权限作为应用程序启动。此外,危险权限在几个组中进行管理。例如,您只需要请求ACCESS_FINE_LOCATION并获得ACCESS_COARSE_LOCATION权限。

#1


1  

May be want to use third party library to simplify permission process, these libraries can help to you.

可能要使用第三方库来简化权限过程,这些库可以对您有所帮助。

PermissionsDispatcher

PermissionsDispatcher

Dexter

德克斯特

RxPermissions

RxPermissions

#2


0  

First declare :

首先声明:

// Assume thisActivity is the current activity

    int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.WRITE_CALENDAR);

Request the permissions you need :

请求您需要的权限:

// 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.
    }
}

Handle the permissions request response:

处理权限请求响应:

@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.

            } else {

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

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

#3


0  

Firstly, you need to classify all these permissions. On Android M version, they are mainly divided to Normal permissions(Like ACCESS_NETWORK_STATE in your Manifest file) and Dangerous permissions(like ACCESS_FINE_LOCATION). For Normal ones you don't have to request them which are granted automatically without a user reminder on installation any more. What you only need to do is just adding them in Manifest!

首先,您需要对所有这些权限进行分类。在Android M版本上,它们主要分为普通权限(如清单文件中的ACCESS_NETWORK_STATE)和危险权限(如ACCESS_FINE_LOCATION)。对于普通的,您不必请求自动授予它们,而不再需要安装用户提醒。你只需要在Manifest中添加它们!

However, Dangerous permissions are more complicated to handle. On app starts up or runs to corresponding feature, you MUST firstly check whether the permissions have been acquired by Context.checkSelfPermission(String permission). Call Context.requestPermissions(String[] permissions, int requestCode) to request by system dialog and get result onRequestPermissionsResult().

但是,危险权限处理起来更复杂。在应用程序启动或运行到相应的功能时,您必须首先检查Context.checkSelfPermission(字符串权限)是否已获取权限。调用Context.requestPermissions(String [] permissions,int requestCode)以通过系统对话框请求并获取结果onRequestPermissionsResult()。

Good news is you can request a bunch of permissions by one API as app launches. Furthermore, Dangerous permissions are managed in several groups. For example, you only need to request for ACCESS_FINE_LOCATION and get ACCESS_COARSE_LOCATION permission as well.

好消息是,您可以通过一个API请求一堆权限作为应用程序启动。此外,危险权限在几个组中进行管理。例如,您只需要请求ACCESS_FINE_LOCATION并获得ACCESS_COARSE_LOCATION权限。