保护应用程序Services
细节
Services通常用于后台处理。像BroadcastReceivers和应用程序Activities一样,应用程序Services可以由外部应用程序调用,因此应受到权限和暴露标志的保护。
建议
Service可以具有多个可从外部调用的方法。可以为每个方法定义任意权限,并通过使用checkPermission()来检查调用包是否具有相应的权限。或者,可以通过使用AndroidManifest中的权限定义方法来单独定义Service和其安全访问权限。
当使用敏感数据调用Service时,验证正在调用的Service是正确的Service而不是恶意Service。如果你知道要连接的组件的确切名称,请在用于连接的Intent中指定该名称。另一种方法是再次使用checkPermission()来验证调用包是否具有接收Intent所需的权限。用户在安装过程中授予应用程序的权限。
以下是自定义权限设置和访问com.example.MyService时所需权限的代码范例。
<permission android:name="com.example.mypermission"
android:label="my_permission" android:protectionLevel="dangerous"></permission>`
<service
android:name="com.example.MyService"
android:permission="com.example.mypermission">
<intent-filter>
<action android:name="com.example.MY_ACTION" />
</intent-filter>
</service>