简体   繁体   English

使用API​​ 22 Android寻求权限

[英]Ask for permissions with API 22 Android

In the project I'm working, we have a set of permissions, and 3 of them, are tagged as dangerous . 在我正在工作的项目中,我们有一组权限,其中3个被标记为dangerous In devices with Android < Android 6.0.0 there are no problems, but with the newest version, the app can't work properly. 在装有Android <Android 6.0.0的设备上没有问题,但是在最新版本下,该应用无法正常运行。

So, searching in google about permissions, I arrived to Google Documentation page, where talk about how to request it: http://developer.android.com/intl/es/training/permissions/requesting.html 因此,在Google中搜索有关权限的信息后,我到达了Google文档页面,其中讨论了如何请求它: http : //developer.android.com/intl/es/training/permissions/requesting.html

They use this code. 他们使用此代码。

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

Where ContextCompat.CheckSelfPermission(..) is only in API 23. 其中ContextCompat.CheckSelfPermission(..)仅在API 23中。

But the problem comes tha for some specifications right now in the project (bosses, etc...) we can't upgrade to API 23 , to use library compats that contains this method. 但是问题出在项目中的某些规范上(老板等),我们不能升级到API 23来使用包含此方法的库兼容性。

SO my question is: 所以我的问题是:

There is any way to handle the request of the permissions, doing some workaround? 有什么方法可以处理权限请求,可以采取一些解决方法?

Permissions: 权限:

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

Take a look at this link 看看这个链接

"The easiest way to manage runtime permissions is by using third-party libraries" PermissionsDispatcher is specially developed for dangerous permissions “管理运行时权限的最简单方法是使用第三方库” PermissionsDispatcher是专门为危险权限而开发的

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

request the permission you need as follow, 要求您获得以下权限,

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

For more details about it you can refer google developer documentation, http://developer.android.com/training/permissions/requesting.html 有关它的更多详细信息,您可以参考Google开发人员文档, http://developer.android.com/training/permissions/requesting.html

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM