4.4.1.2 创建/使用公共服务

公共服务是应该由未指定的大量应用使用的服务。 有必要注意,它可能会收到恶意软件发送的信息(意图等)。 在使用公共服务的情况下,有必要注意,恶意软件可能会收到要发送的信息(意图等)。

下面展示了如何使用startService类型服务的示例代码。

要点(创建服务):

  1. 将导出属性显式设置为true

  2. 小心并安全地处理接收到的意图。

  3. 返回结果时,请勿包含敏感信息。

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="org.jssec.android.service.publicservice" >
  4. <application
  5. android:icon="@drawable/ic_launcher"
  6. android:label="@string/app_name"
  7. android:allowBackup="false" >
  8. <!-- Most standard Service -->
  9. <!-- *** POINT 1 *** Explicitly set the exported attribute to true. -->
  10. <service android:name=".PublicStartService" android:exported="true">
  11. <intent-filter>
  12. <action android:name="org.jssec.android.service.publicservice.action.startservice" />
  13. </intent-filter>
  14. </service>
  15. <!-- Public Service derived from IntentService class -->
  16. <!-- *** POINT 1 *** Explicitly set the exported attribute to true. -->
  17. <service android:name=".PublicIntentService" android:exported="true">
  18. <intent-filter>
  19. <action android:name="org.jssec.android.service.publicservice.action.intentservice" />
  20. </intent-filter>
  21. </service>
  22. </application>
  23. </manifest>

PublicIntentService.java

  1. package org.jssec.android.service.publicservice;
  2. import android.app.IntentService;
  3. import android.content.Intent;
  4. import android.widget.Toast;
  5. public class PublicIntentService extends IntentService{
  6. /**
  7. * Default constructor must be provided when a service extends IntentService class.
  8. * If it does not exist, an error occurs.
  9. */
  10. public PublicIntentService() {
  11. super("CreatingTypeBService");
  12. }
  13. // The onCreate gets called only one time when the Service starts.
  14. @Override
  15. public void onCreate() {
  16. super.onCreate();
  17. Toast.makeText(this, this.getClass().getSimpleName() + " - onCreate()", Toast.LENGTH_SHORT).show();
  18. }
  19. // The onHandleIntent gets called each time after the startService gets called.
  20. @Override
  21. protected void onHandleIntent(Intent intent) {
  22. // *** POINT 2 *** Handle intent carefully and securely.
  23. // Since it's public service, the intent may come from malicious application.
  24. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  25. String param = intent.getStringExtra("PARAM");
  26. Toast.makeText(this, String.format("Recieved parameter ¥"%s¥"", param), Toast.LENGTH_LONG).show();
  27. }
  28. // The onDestroy gets called only one time when the service stops.
  29. @Override
  30. public void onDestroy() {
  31. Toast.makeText(this, this.getClass().getSimpleName() + " - onDestroy()", Toast.LENGTH_SHORT).show();
  32. }
  33. }

下面是使用公共服务的活动代码:

要点(使用服务):

  1. 不要发送敏感信息。

  2. 收到结果时,小心并安全地处理结果数据。

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="org.jssec.android.service.publicserviceuser" >
  4. <application
  5. android:icon="@drawable/ic_launcher"
  6. android:label="@string/app_name"
  7. android:allowBackup="false" >
  8. <activity
  9. android:name=".PublicUserActivity"
  10. android:label="@string/app_name"
  11. android:exported="true">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. </manifest>

PublicUserActivity.java

  1. package org.jssec.android.service.publicserviceuser;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class PublicUserActivity extends Activity {
  7. // Using Service Info
  8. private static final String TARGET_PACKAGE = "org.jssec.android.service.publicservice";
  9. private static final String TARGET_START_CLASS = "org.jssec.android.service.publicservice.PublicStartService";
  10. private static final String TARGET_INTENT_CLASS = "org.jssec.android.service.publicservice.PublicIntentService";
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.publicservice_activity);
  15. }
  16. // --- StartService control ---
  17. public void onStartServiceClick(View v) {
  18. Intent intent = new Intent("org.jssec.android.service.publicservice.action.startservice");
  19. // *** POINT 4 *** Call service by Explicit Intent
  20. intent.setClassName(TARGET_PACKAGE, TARGET_START_CLASS);
  21. // *** POINT 5 *** Do not send sensitive information.
  22. intent.putExtra("PARAM", "Not sensitive information");
  23. startService(intent);
  24. // *** POINT 6 *** When receiving a result, handle the result data carefully and securely.
  25. // This sample code uses startService(), so receiving no result.
  26. }
  27. public void onStopServiceClick(View v) {
  28. doStopService();
  29. }
  30. // --- IntentService control ---
  31. public void onIntentServiceClick(View v) {
  32. Intent intent = new Intent("org.jssec.android.service.publicservice.action.intentservice");
  33. // *** POINT 4 *** Call service by Explicit Intent
  34. intent.setClassName(TARGET_PACKAGE, TARGET_INTENT_CLASS);
  35. // *** POINT 5 *** Do not send sensitive information.
  36. intent.putExtra("PARAM", "Not sensitive information");
  37. startService(intent);
  38. }
  39. @Override
  40. public void onStop(){
  41. super.onStop();
  42. // Stop service if the service is running.
  43. doStopService();
  44. }
  45. // Stop service
  46. private void doStopService() {
  47. Intent intent = new Intent("org.jssec.android.service.publicservice.action.startservice");
  48. // *** POINT 4 *** Call service by Explicit Intent
  49. intent.setClassName(TARGET_PACKAGE, TARGET_START_CLASS);
  50. stopService(intent);
  51. }
  52. }