Android Custom Permission

主要成員:

  • Permission : com.yume190.provider.permission.XXX_API
  • Action : Action.StartService

將service的exportedenabled皆設為true,並透過設定intent-filter去接收特定的action。

宣告方式

CalleeManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<permission-group
android:name="com.yume190.provider.permissions"
android:label="Permission 主標題"
/>


<permission
android:name="com.yume190.provider.permission.XXX_API"
android:permissionGroup="com.yume190.provider.permissions"
android:protectionLevel="normal"
android:label="Permission 副標題"
/>


<application>
<service
android:name="MyService"
android:exported="true"
android:enabled="true"
android:permission="com.yume190.provider.permission.XXX_API"
>

<intent-filter>
<action android:name="Action.StartService" />
</intent-filter>
</service>
</application>

使用方式

CallerManifest.xml
1
<uses-permission android:name="com.yume190.provider.permission.XXX_API" />
CallerMain.java
1
2
Intent intent = new Intent("Action.StartService");
startService(intent);

格式

格式Permission
1
2
3
4
5
6
7
<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"] />

protectionLevel

Value Meaning
“normal” The default value. A lower-risk permission that gives requesting applications access to isolated application-level features, with minimal risk to other applications, the system, or the user. The system automatically grants this type of permission to a requesting application at installation, without asking for the user’s explicit approval (though the user always has the option to review these permissions before installing).
“dangerous” A higher-risk permission that would give a requesting application access to private user data or control over the device that can negatively impact the user. Because this type of permission introduces potential risk, the system may not automatically grant it to the requesting application. For example, any dangerous permissions requested by an application may be displayed to the user and require confirmation before proceeding, or some other approach may be taken to avoid the user automatically allowing the use of such facilities.
“signature” A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user’s explicit approval.
“signatureOrSystem” A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission. Please avoid using this option, as the signature protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The “signatureOrSystem” permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.

Source :