成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

2017-12-21 Android runtime permission 收尾1

fobnn / 3513人閱讀

摘要:無(wú)論是,還是,都必須在文件中。,沒(méi)有授權(quán),執(zhí)行下一步檢查是否需要顯示請(qǐng)求權(quán)限原因。一個(gè)多個(gè)使用申請(qǐng)?zhí)幚矸祷亟Y(jié)果授權(quán)了,執(zhí)行,此處是發(fā)送短信操作。沒(méi)有被授權(quán),給出對(duì)應(yīng)提示信息。

Android runtime permission 收尾1 demo 環(huán)境:

OS: mac OS 10.13.1 (17B1003)

android studio 3.0

gradle 4.1

Code Base

RuntTimePermissionTestActivity.java

classpath "com.android.tools.build:gradle:3.0.0"
compile "com.android.support:appcompat-v7:26.+"

背景 問(wèn)題:app 在 Android 6.0 (API level 23)上,使用SmsManager發(fā)送短信出錯(cuò):
ERROR:   java.lang.SecurityException: Sending SMS message: uid 10078 does not have android.permission.SEND_SMS.
問(wèn)題原因:

查android 官網(wǎng)發(fā)現(xiàn),When run on Android 6.0 (API 23),or targetSdkVersion Android 6.0 (API 23),danger permission需要?jiǎng)討B(tài)申請(qǐng)權(quán)限。

問(wèn)題解決:

方案1 將targetSDKVersion人為地降到小于23,這樣就變成了還是默認(rèn)使用權(quán)限,但是這種并不是Google所推薦使用的。治標(biāo)不治本。

方案2 When Android 6.0 (API 23),or targetSdkVersion Android 6.0 (API 23),實(shí)現(xiàn)APP支持運(yùn)行時(shí)權(quán)限

正文 1. normal permissions and dangerous permissions

Android 6.0 (API 23) 開(kāi)始,引入了normal permissions(普通權(quán)限) 和dangerous permissions(危險(xiǎn)權(quán)限) 的概念。

[PASS]無(wú)論是normal permissions ,還是dangerous permissions ,都必須list在manifest文件中。

[PASS]For dangerous permissions,當(dāng)運(yùn)行OS <=Android 5.1 (API level 22),或者targetSdkVersion <=22時(shí),不需要做特殊的處理。因?yàn)樵诎惭b時(shí)會(huì)用戶必須授權(quán)權(quán)限。如果用戶不授權(quán),system不會(huì)安裝app。

[PASS]For dangerous permissions,當(dāng)運(yùn)行OS >= Android 6.0 (API level 23),或者targetSdkVersion >=23時(shí),要做特殊的處理。使用每個(gè)dangerous permissions之前, 必須request。

From android 官網(wǎng):

On all versions of Android, your app needs to declare both the normal and the dangerous permissions it needs in its app manifest, as described in Declaring Permissions. However, the effect of that declaration is different depending on the system version and your app"s target SDK level:

● If the device is running Android 5.1 (API level 22)or lower, or your app"s target SDKis 22 or lower(targetSdkVersion <=22) : If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all. 
● If the device is running Android 6.0 (API level 23) or higher, and your app"s target SDK is 23 or higher (targetSdkVersion >=23): The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.
Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it"s missing a needed permission, regardless of what API level your app targets.  

Note: Your app still needs to explicitly request every permission it needs, even if the user has already granted another permission in the same group. In addition, the grouping of permissions into groups may change in future Android releases. Your code should not rely on the assumption that particular permissions are or are not in the same group.

https://developer.android.google.cn/training/permissions/requesting.html

2. dangerous permissions

https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

3. For dangerous permissions,when not needs request runtime permission?

When device is running Android 6.0 (API level 23) or higher, and your app"s target SDK is 23 or higher (targetSdkVersion >=23)

Your app only needs permissions for actions that it performs directly.

Your app does not need permission if it is requesting that another app perform the task or provide the information.

4. How to request dangerous permissions? 重要的函數(shù):

checkSelfPermission():檢查是否已經(jīng)具有了相關(guān)權(quán)限。

shouldShowRequestPermissionRationale():判斷是否需要向用戶解釋,為什么需要這些權(quán)限。

requestPermissions() :申請(qǐng)相關(guān)權(quán)限

Sample

RuntTimePermissionTestActivity.java

RuntTimePermissionTestActivity.java 以SEND_SMS(發(fā)送短信)、RECEIVE_SMS(使用SMSReceiver接收短信)、permission_group.SMS 為例子。

android SMS 是什么縮寫(xiě)

SMS是Short Messaging Service(短消息服務(wù))的縮寫(xiě),是一種使用移動(dòng)設(shè)備可以發(fā)送和接收文本信息的技術(shù)。

以例子說(shuō)明如何request runtime permission. Step1,檢查Permission是否granted。
PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getContext(), permission)

true,已經(jīng)授權(quán),直接執(zhí)行doSendMessage。
false,沒(méi)有授權(quán),執(zhí)行下一步檢查是否需要顯示請(qǐng)求權(quán)限原因。

(1) 檢查一個(gè)Permission是否granted:

PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getContext(), Manifest.permission.SEND_SMS)

(2) 檢查多個(gè)Permissions是否granted:

PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getContext(), Manifest.permission.SEND_SMS) 
&&
PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECEIVE_SMS)

(3) 檢查permission_group是否granted:

PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(getContext(), Manifest.permission_group.SMS) 
Step2,檢查Permission是否顯示請(qǐng)求權(quán)限原因。

true,需要顯示請(qǐng)求權(quán)限原因,顯示一個(gè)dialog/Snarkbar,允許用戶做選擇。
false,不需要顯示請(qǐng)求權(quán)限原因,直接執(zhí)行requestPermissions。

(1)檢查一個(gè)Permission是否顯示請(qǐng)求權(quán)限原因:

ActivityCompat.shouldShowRequestPermissionRationale(getContext(), Manifest.permission.SEND_SMS)

(2)檢查多個(gè)Permissions是否顯示請(qǐng)求權(quán)限原因:

ActivityCompat.shouldShowRequestPermissionRationale(getContext(), Manifest.permission.SEND_SMS) 
&&
ActivityCompat.shouldShowRequestPermissionRationale(getContext(), Manifest.permission.RECEIVE_SMS)

(2)檢查permission_group是否顯示請(qǐng)求權(quán)限原因:

ActivityCompat.shouldShowRequestPermissionRationale(getContext(), Manifest.permission_group.SMS)
Step3,Request permission

ActivityCompat.requestPermissions 調(diào)用后,在onRequestPermissionsResult()中根據(jù)requestCode判斷是否grandted,并做對(duì)應(yīng)處理。

public static void requestPermissions(final @NonNull Activity activity,final @NonNull String[] permissions, final @IntRange(from = 0) int requestCode)

說(shuō)明:
(1) permissions:是一個(gè)String[],可以放一個(gè)或多個(gè)permission。
(2) requestCode:在onRequestPermissionsResult()中處理。每次請(qǐng)求request permissions時(shí)requestCode設(shè)置要不同。

(1) request一個(gè)permission:

ActivityCompat.requestPermissions(getContext(), new String[]{Manifest.permission.SEND_SMS}, REQUEST_CODE_4_REQUEST_PERMISSIONS_4_SEND_MESSAGE);

(2) request多個(gè)permissions:

ActivityCompat.requestPermissions(getContext(), new String[]{Manifest.permission.SEND_SMS,Manifest.permission.RECEIVE_SMS}, REQUEST_CODE_4_REQUEST_PERMISSIONS_4_SEND_RECEIVE_MESSAGE);

(3)使用permission_group申請(qǐng):

ActivityCompat.requestPermissions(getContext(), new String[]{Manifest.permission_group.SMS}, REQUEST_CODE_4_REQUEST_PERMISSIONS_GROUP_4_SMS)
Step4 處理 request Permission返回結(jié)果
 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.d(TAG, "onRequestPermissionsResult: requestCode=" + requestCode);
        switch (requestCode) {
            case REQUEST_CODE_4_REQUEST_PERMISSIONS_4_SEND_MESSAGE: {
                onRequestPermissionsResult4SendMessage(requestCode, permissions, grantResults);
                break;
            }

            default:
                // other "case" lines to check for other permissions this app might request
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }

  private void onRequestPermissionsResult4SendMessage(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (REQUEST_CODE_4_REQUEST_PERMISSIONS_4_SEND_MESSAGE != requestCode) {
            Log.e(TAG, "onRequestPermissionsResult4SendMessage: requestCode is wrong");
            return;
        }
        // Check if the only required permission has been granted
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
           // 授權(quán)了,執(zhí)行task,此處是發(fā)送短信操作。
            doSendMessage();
            // Send message permission has been granted, Message can be sent.
            Log.i(TAG, "Send message permission has now been granted. Can send message.");

            // 授權(quán)了,給出對(duì)應(yīng)提示信息。
            showPermissionAvailable4SendMessage();

        } else {
            // permission denied, boo! Disable the functionality that depends on this permission.
            Log.i(TAG, "Send message permission was NOT granted.");
           // 沒(méi)有被授權(quán),給出對(duì)應(yīng)提示信息。
            showPermissionsNotGranted();
        }
    }
5. request runtime permission 流程總結(jié)

6 總結(jié)

不要使用Permission Group來(lái)request dangerous permissions。原因是將來(lái)API 不同,permissions groups可能不同。

僅僅申請(qǐng)需要的權(quán)限,申請(qǐng)?zhí)嗖灰膁angerous permissions可能會(huì)使得用戶感覺(jué)app不安全,導(dǎo)致用戶卸載app。

Show dangerous permissions rationale,簡(jiǎn)潔明了。

任何時(shí)候APP都要在執(zhí)行危險(xiǎn)權(quán)限前去檢查是否具有相關(guān)權(quán)限,即使剛剛執(zhí)行過(guò)這項(xiàng)操作,因?yàn)橛脩艉苡锌赡苋ピO(shè)置應(yīng)用中關(guān)閉了相關(guān)權(quán)限。

[pass]申請(qǐng)權(quán)限時(shí),如果組內(nèi)有別的權(quán)限已經(jīng)獲得了用戶授權(quán),系統(tǒng)不再?gòu)棾鲈儐?wèn)對(duì)話框,而是自動(dòng)授權(quán)該權(quán)限。

例如,申請(qǐng)Manifest.permission.SEND_SMS權(quán)限時(shí),用戶已經(jīng)授權(quán)了Manifest.permission.SEND_SMS權(quán)限,系統(tǒng)則會(huì)自動(dòng)授權(quán)group SMS中的所有權(quán)限,不再詢問(wèn)用戶;

兼容問(wèn)題:Use the Android Support Library to check for, and request, permissions.

checkSelfPermission和requestPermissions從API 23才加入,低于23版本,需要在運(yùn)行時(shí)判斷。

使用Support Library v4中提供的方法,可以避免判斷。

ContextCompat.checkSelfPermission
ActivityCompat.requestPermissions
ActivityCompat.shouldShowRequestPermissionRationale

例如:使用ContextCompat.checkSelfPermission , 而不是Activity.checkSelfPermission()。google 官網(wǎng)推薦這種方式。

6 遺留的問(wèn)題:

Android官方開(kāi)發(fā)指導(dǎo)還提到一點(diǎn),為避免給用戶帶來(lái)糟糕的用戶體驗(yàn),shouldShowRequestPermissionRationale 這里的解釋說(shuō)明應(yīng)該是異步的,不要阻塞用戶的操作。
沒(méi)有看懂是什么意思。
大家知道了,可以告訴我,我會(huì)非常感謝。

7 下一步的計(jì)劃:

準(zhǔn)備創(chuàng)建一個(gè)BaseActivity,或一個(gè)工具類(lèi),把request runtime permission 代碼抽離。

寫(xiě)關(guān)于Runtime permission library的總結(jié)。

Ref

https://developer.android.com/training/permissions/index.html

https://developer.android.com/training/permissions/declaring.html

https://developer.android.com/training/permissions/requesting.html

-https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous
PS: 中國(guó)訪問(wèn), 把com -> google.cn 就可以打開(kāi)了。

http://www.jianshu.com/p/0beb6243d650

http://www.jianshu.com/p/e1ab1a179fbb/

https://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

http://www.jianshu.com/p/f346b7446610

EasyPermissions

https://www.cnblogs.com/whycxb/p/6818685.html

rxpermissions

https://github.com/tbruyelle/RxPermissions

android send sms

http://www.cnblogs.com/huhx/p/sendMessage.html

snackbars:

http://wiki.jikexueyuan.com/project/material-design/components/snackbars-and-toasts.html

http://blog.csdn.net/qq_22706515/article/details/51151654

http://blog.csdn.net/sdjianfei/article/details/51583023

Android Support v4,v7,v13的區(qū)別以及 v4,v7包沖突問(wèn)題

http://blog.csdn.net/shuaiyou_comon/article/details/75425639?locationNum=5&fps=1

謝謝瀏覽

歡迎大家交流,留言、指點(diǎn)。

文中有誤,歡迎大家指出來(lái)。我會(huì)更正過(guò)來(lái)。

我的GitHub

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70876.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<