一、注意事项
1、显式设置exported属性为false。@b@2、安全处理收到的intent,确认其真实性。@b@3、敏感数据可以在同一个应用中发送和请求。
二、原代码示例
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>@b@ @b@<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.jssec.android.service.privateservice" >@b@ @b@ <application@b@ android:icon="@drawable/ic_launcher"@b@ android:label="@string/app_name"@b@ android:allowBackup="false" >@b@ <activity@b@ android:name=".PrivateUserActivity"@b@ android:label="@string/app_name"@b@ android:exported="true" >@b@ <intent-filter>@b@ <action android:name="android.intent.action.MAIN" />@b@ <category android:name="android.intent.category.LAUNCHER" />@b@ </intent-filter>@b@ </activity>@b@ @b@ <!-- Private Service derived from Service class -->@b@ <!-- *** POINT 1 *** Explicitly set the exported attribute to false. -->@b@ <service android:name=".PrivateStartService" android:exported="false"/>@b@ @b@ <!-- Private Service derived from IntentService class -->@b@ <!-- *** POINT 1 *** Explicitly set the exported attribute to false. -->@b@ <service android:name=".PrivateIntentService" android:exported="false"/>@b@ @b@ </application>@b@</manifest>
2.PrivateStartService.java
package org.jssec.android.service.privateservice;@b@ @b@import android.app.Service;@b@import android.content.Intent;@b@import android.os.IBinder;@b@import android.widget.Toast;@b@ @b@public class PrivateStartService extends Service {@b@ @b@ // The onCreate gets called only one time when the service starts.@b@ @Override@b@ public void onCreate() {@b@ Toast.makeText(this, "PrivateStartService - onCreate()", Toast.LENGTH_SHORT).show();@b@ }@b@ @b@ // The onStartCommand gets called each time after the startService gets called.@b@ @Override@b@ public int onStartCommand(Intent intent, int flags, int startId) {@b@ // *** POINT 2 *** Handle the received intent carefully and securely,@b@ // even though the intent was sent from the same application.@b@ // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@ String param = intent.getStringExtra("PARAM");@b@ Toast.makeText(this,@b@ String.format("PrivateStartService¥nReceived param: ¥"%s¥"", param), Toast.LENGTH_LONG).show();@b@ return Service.START_NOT_STICKY;@b@ }@b@ @b@ // The onDestroy gets called only one time when the service stops. @Override@b@ public void onDestroy() {@b@ Toast.makeText(this, "PrivateStartService - onDestroy()", Toast.LENGTH_SHORT).show();@b@ }@b@ @b@ @Override@b@ public IBinder onBind(Intent intent) { @b@ // This service does not provide binding, so return null@b@ return null;@b@ }@b@}
3.安全使用PrivateUserActivity.java - (1、在同一个程序中,使用显式intent调用service、2、第三信息可以发送给同一个应用中的目标service、3、处理收到的结果数据,确认真实性和可用性)
package org.jssec.android.service.privateservice;@b@ @b@import android.app.Activity;@b@import android.content.Intent;@b@import android.os.Bundle;@b@import android.view.View;@b@ @b@public class PrivateUserActivity extends Activity {@b@ @b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ setContentView(R.layout.privateservice_activity);@b@ }@b@ @b@ // --- StartService control ---@b@ @b@ public void onStartServiceClick(View v) {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateStartService.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination service is in the same application.@b@ intent.putExtra("PARAM", "Sensitive information");@b@ @b@ startService(intent);@b@ }@b@ @b@ public void onStopServiceClick(View v) {@b@ doStopService();@b@ }@b@ @b@ @Override@b@ public void onStop() {@b@ super.onStop();@b@ // Stop service if the service is running.@b@ doStopService();@b@ }@b@ @b@ private void doStopService() {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateStartService.class);@b@ stopService(intent);@b@ }@b@ @b@ // --- IntentService control ---@b@ @b@ public void onIntentServiceClick(View v) {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateIntentService.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination service is in the same application.@b@ intent.putExtra("PARAM", "Sensitive information");@b@ @b@ startService(intent);@b@ }@b@}
三、安全代码示例
1、在同一个程序中,使用显式intent调用service。@b@2、第三信息可以发送给同一个应用中的目标service。@b@3、处理收到的结果数据,确认真实性和可用性。
PrivateUserActivity.java@b@ @b@package org.jssec.android.service.privateservice;@b@ @b@import android.app.Activity;@b@import android.content.Intent;@b@import android.os.Bundle;@b@import android.view.View;@b@ @b@public class PrivateUserActivity extends Activity {@b@ @b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ setContentView(R.layout.privateservice_activity);@b@ }@b@ @b@ // --- StartService control ---@b@ @b@ public void onStartServiceClick(View v) {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateStartService.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination service is in the same application.@b@ intent.putExtra("PARAM", "Sensitive information");@b@ @b@ startService(intent);@b@ }@b@ @b@ public void onStopServiceClick(View v) {@b@ doStopService();@b@ }@b@ @b@ @Override@b@ public void onStop() {@b@ super.onStop();@b@ // Stop service if the service is running.@b@ doStopService();@b@ }@b@ @b@ private void doStopService() {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateStartService.class);@b@ stopService(intent);@b@ }@b@ @b@ // --- IntentService control ---@b@ @b@ public void onIntentServiceClick(View v) {@b@ // *** POINT 4 *** Use the explicit intent with class specified to call a service in the same application.@b@ Intent intent = new Intent(this, PrivateIntentService.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination service is in the same application.@b@ intent.putExtra("PARAM", "Sensitive information");@b@ @b@ startService(intent);@b@ }@b@}