欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android權(quán)限控制之自定義權(quán)限

 更新時(shí)間:2015年04月07日 10:03:43   投稿:junjie  
這篇文章主要介紹了Android權(quán)限控制之自定義權(quán)限,本文使用兩個(gè)APP作為范例,講解如何自定義權(quán)限,需要的朋友可以參考下

天哪,這篇文章終于說道如何自定義權(quán)限了,左盼右盼,其實(shí)這個(gè)自定義權(quán)限相當(dāng)easy。為了方便敘述,我這邊會用到兩個(gè)app作為例子示范。

Permission App: used to define a new permission
這個(gè)作為定義權(quán)限的App,我稱之為Permission App.
Client App: used to access the specified activity of Permission App
這個(gè)作為訪問上述自定義權(quán)限的App,我稱之為Client App

先看如何寫Permission App

第一步

Permission App很簡單,它的任務(wù)就是定一個(gè)Permission,使用< permission>標(biāo)簽即可,我們假設(shè)內(nèi)容如下:

復(fù)制代碼 代碼如下:

<permission android:name="custom.permission.STARTACTIVITY" android:description="@string/permission_dcr" android:protectionLevel=signatureOrSystem android:label="label"></permission>

第二步

然后在定一個(gè)Activity,這個(gè)Activity很簡單就是展示下一行字,如”Hello from Custiom Permission Activity!”這里就不詳述。

第三步

最重要的地方:我們需要為這個(gè)Activity指明訪問權(quán)限,權(quán)限即為我們剛申請的權(quán)限,這個(gè)需要同樣需要在AndroidManifest.xml文件中標(biāo)識,如下:

復(fù)制代碼 代碼如下:

<activity
        android:name="com.example.custompermission.MainActivity"
        android:label="@string/app_name" android:permission="custom.permission.STARTACTIVITY"> 
</activity>

這個(gè)Activity于是就被打上了必須使用” custom.permission.STARTACTIVITY”權(quán)限才能訪問的印記。

接著寫Client App

至于如何寫Client App,那就so so so … easy了,只需兩步:

第一步

在AndroidManifest.xml文件中首先申請權(quán)限,如下:

復(fù)制代碼 代碼如下:

<uses-permission android:name="custom.permission.STARTACTIVITY"/>

第二步

訪問Permission App表明需要該權(quán)限的Activity,代碼如下:

復(fù)制代碼 代碼如下:

Intent in = new Intent(); 
in.setClassName("com.example.custompermission", "com.example.custompermission.MainActivity"); 
startActivity(in);

大功告成

我們可以測試下效果,首先安裝Permission App,然后接著安裝Client App,結(jié)果如下:

點(diǎn)擊之后

另外我曾經(jīng)在Android Permission權(quán)限機(jī)制引子提到過Protection Level問題,這邊我同樣測試下這個(gè)Protection Level,下面結(jié)果中Y表示可以正常訪問,N則表示不可以訪問。

需要注意的是,使用自定義Permission的activity如果設(shè)置了:

復(fù)制代碼 代碼如下:

<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,因?yàn)橹挥心愕腖auncher必須使用了uses-permission去請求獲取custom.permission.STARTACTIVITY權(quán)限,事實(shí)上你的Launcher是不具備已經(jīng)請求自定義權(quán)限的。

Launcher會報(bào):Application is not installed on your phone. 的錯(cuò)誤.

相關(guān)文章

最新評論