uses-permission和permission詳解


1.<uses-permission>:

官方描述:

If an application needs access to a feature protected by a permission, it must declare that it requires that permission with a  <uses-permission>  
element in the manifest. Then, when the application is installed on the device, the installer determines whether or not to grant the requested
permission by checking the authorities that signed the application's certificates and, in some cases, asking the user. If the permission is granted,
the application is able to use the protected features. If not, its attempts to access those features will simply fail without any notification to the user.

 

如果一個應用需要訪問一個受permission保護的特性,那這個應用必須在 manifest中以 <uses-permission> 節點聲明它所需要的權限。當這個應用安裝在設備的時候,

安裝器會決定是否授予它所聲明的權限,這有時候會詢問用戶。如果權限被授予了,這個應用才能使用受保護的特性,否則的話, 訪問失敗並且 不會 通知用戶。

注意:不一定是調用其他應用程序才要聲明 <uses-permission> ,有時甚至調用自己應用的程序的組件都要聲明!!!(下面的例子會說到)

2. <permission>:

An application can also protect its own components (activities, services, broadcast receivers, and content providers) with permissions. 
It can employ any of the permissions defined by Android (listed in android.Manifest.permission ) or declared by other applications. Or
it can define its own. A new permission is declared with the <permission> element.

 

一個應用程序也能用 permissions 保護自己的組件,它能使用android系統定義的或者其他應用定義的又或者自身應用定義的permissions,如果要想定義一個新的permission,

可以用 <permission>  節點來定義。如下:

<permission android:description="string resource"
  android:icon="drawable resource"
  android:label="string resource"
  android:name="string"
  android:permissionGroup="string"
  android:protectionLevel=["normal" | "dangerous" | 
       "signature" | "signatureOrSystem"] />

For example, an activity could be protected as follows:

<manifest . . . >
  <permission android:name="com.example.project.DEBIT_ACCT" . . . />
  <uses-permission android:name="com.example.project.DEBIT_ACCT" />
  . . .
  <application . . .>
    <activity android:name="com.example.project.FreneticActivity"
          android:permission="com.example.project.DEBIT_ACCT"
          . . . >
      . . .
    </activity>
  </application>
</manifest>
Note that, in this example, the DEBIT_ACCT permission is not only declared with the  <permission> element, its use is also requested with the  <uses-permission>element.
Its use must be requested in order for other components of the application to launch the protected activity, even though the protection is imposed by the application itself. If, in the same example, the permission attribute was set to a permission declared elsewhere (such as android.permission.CALL_EMERGENCY_NUMBERS , it would not have been
necessary to declare it again with a
<permission> element. However, it would still have been necessary to request its use with <uses-permission> .

 

注意的是,在這個例子中, DEBIT_ACCT這個權限不僅在<permission>中聲明,並且也在<uses-permission>中聲明,要想在這應用的其他組件啟動這個受保護的activity時,

在<uses-permission>中聲明DEBIT_ACCT這個權限是必須的,即使這個保護是這個應用本身加上的。(這印證了上面第1點說的)。

注意,如果添加的permission是其他地方定義的,那就沒必要再<permission>聲明一次,但是,仍然用<uses-permission>聲明這個權限。

參考:

http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms

http://berdy.iteye.com/blog/1782854

http://blog.csdn.net/lilu_leo/article/details/6940941


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM