使用API​​ 22 Android

时间:2016-03-17 09:41:36

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

在我正在工作的项目中,我们有一组权限,其中3个被标记为dangerous。在使用Android<的设备中Android 6.0.0没有问题,但是使用最新版本,应用程序无法正常工作。

因此,在谷歌搜索权限时,我访问了Google文档页面,其中讨论了如何请求它:http://developer.android.com/intl/es/training/permissions/requesting.html

他们使用此代码。

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) { ... }

ContextCompat.CheckSelfPermission(..)仅适用于API 23。

但问题出现在项目中的某些规范(老板等等)我们无法升级到API 23,使用包含此方法的库compats。

我的问题是:

有什么方法可以处理权限请求,做一些解决方法吗?

权限:

<uses-permission android:name="xx.xx.otr.app.providers.imps.permission.READ_ONLY" />
    <uses-permission android:name="xx.xx.otr.app.providers.imps.permission.WRITE_ONLY" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="es.in2.otr.app.im.permission.IM_SERVICE" />
    <uses-permission android:name="android.permission.UPDATE_APP_OPS_STATS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="com.google.android.googleapps.permission.GOOGLE_AUTH" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="xx.xx.otr.app.im.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="xx.xx.otr.app.im.permission.C2D_MESSAGE" />

    <permission
        android:name="xx.xx.otr.app.im.permission.IM_SERVICE"
        android:description="@string/perm_desc"
        android:label="@string/perm_label"
        android:permissionGroup="android.permission-group.MESSAGES"
        android:protectionLevel="dangerous" />
    <permission
        android:name="xx.xx.otr.app.providers.imps.permission.READ_ONLY"
        android:description="@string/ro_perm_desc"
        android:label="@string/ro_perm_label"
        android:permissionGroup="android.permission-group.MESSAGES"
        android:protectionLevel="dangerous" />
    <permission
        android:name="xx.xx.otr.app.providers.imps.permission.WRITE_ONLY"
        android:description="@string/wo_perm_desc"
        android:label="@string/wo_perm_label"
        android:permissionGroup="android.permission-group.MESSAGES"
        android:protectionLevel="dangerous" />

2 个答案:

答案 0 :(得分:2)

看一下这个链接

“管理运行时权限的最简单方法是使用第三方库” PermissionsDispatcher是专为危险权限而开发的

https://guides.codepath.com/android/Managing-Runtime-Permissions-with-PermissionsDispatcher

答案 1 :(得分:0)

请求您需要的权限,如下所示

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


Get the response of the permission as follows, 
@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
    }
}

有关它的更多详细信息,您可以参考google开发者文档, http://developer.android.com/training/permissions/requesting.html

希望这会对你有所帮助。