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

时间:2016-06-16 09:31:36

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

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

清单权限

<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 个答案:

答案 0 :(得分:1)

可能想要使用第三方库来简化权限过程,这些库可以帮到你。

PermissionsDispatcher

Dexter

RxPermissions

答案 1 :(得分:0)

首先声明:

// Assume thisActivity is the current activity

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

请求您需要的权限:

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

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

答案 2 :(得分:0)

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

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

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