語法(SYNTAX):
<uses-permissionandroid:name="string"/>
被包含於(CONTAINED IN):
<manifest>
說明(DESCRIPTION):
這個屬性用於給應用程序授予正確的操作的所必須的權限。這些權限是在應用程序安裝時被授予的,而不是在運行時授予的。
有關更多的權限信息,請看《AndroidManifest.xml文件詳解(三)》文檔中“權限”介紹(http://blog.csdn.net/fireofstar/article/details/7543067)和《Android的安全性和權限》(http://blog.csdn.net/fireofstar/article/details/7536803)。在android.Manifest.permission類中能夠找到由基礎平台定義的一個權限列表(http://developer.android.com/reference/android/Manifest.permission.html)。
屬性(ATTRIBUTES):
android:name
這個屬性用於定義權限的 名稱。它能夠是由該應用程序用<permission>元素定義的一個權限,也可以是由另外一個應用程序所定義的權限,還可以是有系統定義的 標准的權限,如:android:permission.CAMERA或android:permission.READ_CONTACTS等。就像例子 中所顯示的那樣,權限名通常要用包名做為前綴,以保證其唯一性。
被引入的版本(INTRODUCED IN):
API Level 1
應用程序自定義 <permission>
天哪,這篇文章終於說道如何自定義權限了,左盼右盼,其實這個自定義權限相當easy。為了方便敘述,我這邊會用到兩個app作為例子示范。
Permission App: used to define a new permission
這個作為定義權限的App,我稱之為Permission App.
Client App: used to access the specified activity of Permission App
這個作為訪問上述自定義權限的App,我稱之為Client App
先看如何寫Permission App
第一步
Permission App很簡單,它的任務就是定一個Permission,使用< permission>標簽即可,我們假設內容如下:
<permission
第二步
然后在定一個Activity,這個Activity很簡單就是展示下一行字,如”Hello from Custiom Permission Activity!”這里就不詳述。
第三步
最重要的地方:我們需要為這個Activity指明訪問權限,權限即為我們剛申請的權限,這個需要同樣需要在AndroidManifest.xml文件中標識,如下:
<activity
android:name="com.example.custompermission.MainActivity"
android:label="@string/app_name"
</activity>
這個Activity於是就被打上了必須使用” custom.permission.STARTACTIVITY”權限才能訪問的印記。
接着寫Client App
至於如何寫Client App,那就so so so … easy了,只需兩步:
第一步
在AndroidManifest.xml文件中首先申請權限,如下:
<uses-permission android:name="custom.permission.STARTACTIVITY"/>
第二步
訪問Permission App表明需要該權限的Activity,代碼如下:
Intent in = new Intent();
startActivity(in);
大功告成
我們可以測試下效果,首先安裝Permission App,然后接着安裝Client App,結果如下:
點擊之后
另外我曾經在Android Permission權限機制引子提到過Protection Level問題,這邊我同樣測試下這個Protection Level,下面結果中Y表示可以正常訪問,N則表示不可以訪問。
需要注意的是,使用自定義Permission的activity如果設置了
android:name="com.example.custompermission.MainActivity"
android:label="@string/app_name" android:permission="custom.permission.STARTACTIVITY">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
那就不能從Launcher中啟動該App,因為只有你的Launcher必須使用了uses-permission去請求獲取custom.permission.STARTACTIVITY權限,事實上你的Launcher是不具備已經請求自定義權限的。
Launcher會報:Application is not installed on your phone. 的錯誤.