4.2.1.2 公共广播接收器

公共广播接收器是可以从未指定的大量应用程序接收广播的广播接收器,因此有必要注意,它可能从恶意软件接收广播。

要点(接收广播):

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

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

  3. 返回结果时,不要包含敏感信息。

公共广播接收器的示例代码可以用于静态和动态广播接收器。

PublicReceiver.java

  1. package org.jssec.android.broadcast.publicreceiver;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.widget.Toast;
  7. public class PublicReceiver extends BroadcastReceiver {
  8. private static final String MY_BROADCAST_PUBLIC =
  9. "org.jssec.android.broadcast.MY_BROADCAST_PUBLIC";
  10. public boolean isDynamic = false;
  11. private String getName() {
  12. return isDynamic ? "Public Dynamic Broadcast Receiver" : "Public Static Broadcast Receiver";
  13. }
  14. @Override
  15. public void onReceive(Context context, Intent intent) {
  16. // *** POINT 2 *** Handle the received Intent carefully and securely.
  17. // Since this is a public broadcast receiver, the requesting application may be malware.
  18. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  19. if (MY_BROADCAST_PUBLIC.equals(intent.getAction())) {
  20. String param = intent.getStringExtra("PARAM");
  21. Toast.makeText(context,
  22. String.format("%s:¥nReceived param: ¥"%s¥"", getName(), param),
  23. Toast.LENGTH_SHORT).show();
  24. }
  25. // *** POINT 3 *** When returning a result, do not include sensitive information.
  26. // Since this is a public broadcast receiver, the requesting application may be malware.
  27. // If no problem when the information is taken by malware, it can be returned as result.
  28. setResultCode(Activity.RESULT_OK);
  29. setResultData(String.format("Not Sensitive Info from %s", getName()));
  30. abortBroadcast();
  31. }
  32. }

静态广播接收器定义在AndroidManifest.xml中:

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.broadcast.publicreceiver" >
  4. <application
  5. android:icon="@drawable/ic_launcher"
  6. android:label="@string/app_name"
  7. android:allowBackup="false" >
  8. <!-- Public Static Broadcast Receiver -->
  9. <!-- *** POINT 1 *** Explicitly set the exported attribute to true. -->
  10. <receiver
  11. android:name=".PublicReceiver"
  12. android:exported="true" >
  13. <intent-filter>
  14. <action android:name="org.jssec.android.broadcast.MY_BROADCAST_PUBLIC" />
  15. </intent-filter>
  16. </receiver>
  17. <service
  18. android:name=".DynamicReceiverService"
  19. android:exported="false" />
  20. <activity
  21. android:name=".PublicReceiverActivity"
  22. android:label="@string/app_name"
  23. android:exported="true" >
  24. <intent-filter>
  25. <action android:name="android.intent.action.MAIN" />
  26. <category android:name="android.intent.category.LAUNCHER" />
  27. </intent-filter>
  28. </activity>
  29. </application>
  30. </manifest>

在动态广播接收器中,通过调用程序中的registerReceiver()unregisterReceiver()来执行注册/注销。 为了通过按钮操作执行注册/注销,该按钮PublicReceiverActivity中定义。 由于动态广播接收器实例的作用域比PublicReceiverActivity长,因此不能将其保存为PublicReceiverActivity的成员变量。 在这种情况下,请将动态广播接收器实例保存为DynamicReceiverService的成员变量,然后从PublicReceiverActivity启动/结束DynamicReceiverService,来间接注册/注销动态广播接收器。

DynamicReceiverService.java

  1. package org.jssec.android.broadcast.publicreceiver;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.os.IBinder;
  6. import android.widget.Toast;
  7. public class DynamicReceiverService extends Service {
  8. private static final String MY_BROADCAST_PUBLIC =
  9. "org.jssec.android.broadcast.MY_BROADCAST_PUBLIC";
  10. private PublicReceiver mReceiver;
  11. @Override
  12. public IBinder onBind(Intent intent) {
  13. return null;
  14. }
  15. @Override
  16. public void onCreate() {
  17. super.onCreate();
  18. // Register Public Dynamic Broadcast Receiver.
  19. mReceiver = new PublicReceiver();
  20. mReceiver.isDynamic = true;
  21. IntentFilter filter = new IntentFilter();
  22. filter.addAction(MY_BROADCAST_PUBLIC);
  23. filter.setPriority(1); // Prioritize Dynamic Broadcast Receiver, rather than Static Broadcast Receiver.
  24. registerReceiver(mReceiver, filter);
  25. Toast.makeText(this,
  26. "Registered Dynamic Broadcast Receiver.",
  27. Toast.LENGTH_SHORT).show();
  28. }
  29. @Override
  30. public void onDestroy() {
  31. super.onDestroy();
  32. // Unregister Public Dynamic Broadcast Receiver.
  33. unregisterReceiver(mReceiver);
  34. mReceiver = null;
  35. Toast.makeText(this,
  36. "Unregistered Dynamic Broadcast Receiver.",
  37. Toast.LENGTH_SHORT).show();
  38. }
  39. }

PublicReceiverActivity.java

  1. package org.jssec.android.broadcast.publicreceiver;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class PublicReceiverActivity extends Activity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. }
  12. public void onRegisterReceiverClick(View view) {
  13. Intent intent = new Intent(this, DynamicReceiverService.class);
  14. startService(intent);
  15. }
  16. public void onUnregisterReceiverClick(View view) {
  17. Intent intent = new Intent(this, DynamicReceiverService.class);
  18. stopService(intent);
  19. }
  20. }

接下来,展示了将广播发送到公共广播接收器的示例代码。 当向公共广播接收器发送广播时,需要注意广播可以被恶意软件接收。

要点(发送广播):

  1. 不要发送敏感信息

  2. 接受广播时,小心并安全地处理结果数据

PublicSenderActivity.java

  1. package org.jssec.android.broadcast.publicsender;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.TextView;
  9. public class PublicSenderActivity extends Activity {
  10. private static final String MY_BROADCAST_PUBLIC =
  11. "org.jssec.android.broadcast.MY_BROADCAST_PUBLIC";
  12. public void onSendNormalClick(View view) {
  13. // *** POINT 4 *** Do not send sensitive information.
  14. Intent intent = new Intent(MY_BROADCAST_PUBLIC);
  15. intent.putExtra("PARAM", "Not Sensitive Info from Sender");
  16. sendBroadcast(intent);
  17. }
  18. public void onSendOrderedClick(View view) {
  19. // *** POINT 4 *** Do not send sensitive information.
  20. Intent intent = new Intent(MY_BROADCAST_PUBLIC);
  21. intent.putExtra("PARAM", "Not Sensitive Info from Sender");
  22. sendOrderedBroadcast(intent, null, mResultReceiver, null, 0, null, null);
  23. }
  24. public void onSendStickyClick(View view) {
  25. // *** POINT 4 *** Do not send sensitive information.
  26. Intent intent = new Intent(MY_BROADCAST_PUBLIC);
  27. intent.putExtra("PARAM", "Not Sensitive Info from Sender");
  28. //sendStickyBroadcast is deprecated at API Level 21
  29. sendStickyBroadcast(intent);
  30. }
  31. public void onSendStickyOrderedClick(View view) {
  32. // *** POINT 4 *** Do not send sensitive information.
  33. Intent intent = new Intent(MY_BROADCAST_PUBLIC);
  34. intent.putExtra("PARAM", "Not Sensitive Info from Sender");
  35. //sendStickyOrderedBroadcast is deprecated at API Level 21
  36. sendStickyOrderedBroadcast(intent, mResultReceiver, null, 0, null, null);
  37. }
  38. public void onRemoveStickyClick(View view) {
  39. Intent intent = new Intent(MY_BROADCAST_PUBLIC);
  40. //removeStickyBroadcast is deprecated at API Level 21
  41. removeStickyBroadcast(intent);
  42. }
  43. private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
  44. @Override
  45. public void onReceive(Context context, Intent intent) {
  46. // *** POINT 5 *** When receiving a result, handle the result data carefully and securely.
  47. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  48. String data = getResultData();
  49. PublicSenderActivity.this.logLine(
  50. String.format("Received result: ¥"%s¥"", data));
  51. }
  52. };
  53. private TextView mLogView;
  54. @Override
  55. public void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.main);
  58. mLogView = (TextView)findViewById(R.id.logview);
  59. }
  60. private void logLine(String line) {
  61. mLogView.append(line);
  62. mLogView.append("¥n");
  63. }
  64. }