Android 程序申請(qǐng)權(quán)限注意事項(xiàng)
為Android 程序申請(qǐng)權(quán)限注意
Android系統(tǒng)提供為程序提供了權(quán)限申請(qǐng),即在manifest中使用uses-permission來(lái)申請(qǐng)即可.實(shí)現(xiàn)起來(lái)非常簡(jiǎn)單,但是有些問(wèn)題會(huì)隨之浮出水面. 常見(jiàn)的現(xiàn)象是,有時(shí)候新加一個(gè)權(quán)限,(在Google Play上)程序顯示的支持的設(shè)備會(huì)減少.
為什么權(quán)限越多,支持設(shè)備越少
因?yàn)橛行?quán)限隱式地需要feature,即當(dāng)你顯示使用uses-permission,會(huì)默認(rèn)地為程序加入uses-feature.
而Android以及Google Play判斷是否可以安裝和現(xiàn)實(shí)的依據(jù)是,設(shè)備包含的system features是否完全包含程序申請(qǐng)的全部features. 只有在全部滿足了程序需要的feature的設(shè)備上才可以展示并安裝.
如何查看程序使用了哪些features
使用aapt dump badging your_apk_file_path,具體可以參考獲取程序需要的features
如何查看設(shè)備具有的features
Android提供了該API,具體參考獲取系統(tǒng)支持的features
舉個(gè)例子
我們?cè)诔绦騧anifest加入一行申請(qǐng)攝像頭的權(quán)限.
<uses-permission android:name="android.permission.CAMERA" />
然后查看程序加入的feature
14:29 $ aapt dump badging PermissionDemo.apk | grep uses-feature
我們就會(huì)發(fā)現(xiàn),這兩個(gè)權(quán)限是新加的
uses-feature:'android.hardware.camera'
uses-feature:'android.hardware.camera.autofocus'
解決問(wèn)題:如何加權(quán)限,不減少支持設(shè)備
如果你增加的權(quán)限并且及引入的feature不是必須使用的,可以顯示地將該feature設(shè)置為不需要.繼續(xù)上面的例子.在manifest中加入
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
重新生成程序.再次查看需要的權(quán)限.
14:29 $ aapt dump badging PermissionDemo.apk | grep uses-feature
uses-feature-not-required:'android.hardware.camera.autofocus'
uses-feature-not-required:'android.hardware.camera'
uses-feature:'android.hardware.touchscreen'
就這樣,可以做到增加權(quán)限,同時(shí)保證支持設(shè)備不減少.
Show Me The Code
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.droidyue.demo.permission" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> <uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > </application> </manifest>
延伸閱讀
http://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions
以上就是對(duì)Android 程序申請(qǐng)權(quán)限的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
在Android打包中區(qū)分測(cè)試和正式環(huán)境淺析
這篇文章主要給大家介紹了關(guān)于在Android打包中如何區(qū)分測(cè)試和正式環(huán)境的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧。2017-10-10Android中日期與時(shí)間設(shè)置控件用法實(shí)例
這篇文章主要介紹了Android中日期與時(shí)間設(shè)置控件用法,實(shí)例分析了Android日期與時(shí)間相關(guān)控件的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Android仿簡(jiǎn)書(shū)搜索框效果的示例代碼
本篇文章主要介紹了Android仿簡(jiǎn)書(shū)搜索框效果的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼
這篇文章主要介紹了Android調(diào)節(jié)屏幕亮度實(shí)現(xiàn)代碼,調(diào)節(jié)屏幕亮度時(shí),先設(shè)置當(dāng)前activity亮度,再并保存為系統(tǒng)亮度即可,本文分別給出兩個(gè)步驟的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05Android開(kāi)發(fā)實(shí)現(xiàn)切換主題及換膚功能示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)切換主題及換膚功能,涉及Android界面布局與樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03深入學(xué)習(xí)Kotlin?枚舉的簡(jiǎn)潔又高效進(jìn)階用法
這篇文章主要為大家介紹了深入學(xué)習(xí)Kotlin?枚舉簡(jiǎn)潔又高效的進(jìn)階用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Android使用xUtils3.0實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Android使用xUtils3.0實(shí)現(xiàn)文件上傳的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11