4.2.1.1 私有广播接收器

私人广播接收器是最安全的广播接收器,因为只能接收到从应用内发送的广播。 动态广播接收器不能注册为私有,所以私有广播接收器只包含静态广播接收器。

要点(接收广播):

  1. 将导出属性显示设为false

  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.broadcast.privatereceiver" >
  4. <application
  5. android:icon="@drawable/ic_launcher"
  6. android:label="@string/app_name"
  7. android:allowBackup="false" >
  8. <!-- Private Broadcast Receiver -->
  9. <!-- *** POINT 1 *** Explicitly set the exported attribute to false. -->
  10. <receiver
  11. android:name=".PrivateReceiver"
  12. android:exported="false" />
  13. <activity
  14. android:name=".PrivateSenderActivity"
  15. android:label="@string/app_name"
  16. android:exported="true" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

PrivateReceiver.java

  1. package org.jssec.android.broadcast.privatereceiver;
  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 PrivateReceiver extends BroadcastReceiver {
  8. @Override
  9. public void onReceive(Context context, Intent intent) {
  10. // *** POINT 2 *** Handle the received intent carefully and securely,
  11. // even though the intent was sent from within the same application.
  12. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  13. String param = intent.getStringExtra("PARAM");
  14. Toast.makeText(context,
  15. String.format("Received param: ¥"%s¥"", param),
  16. Toast.LENGTH_SHORT).show();
  17. // *** POINT 3 *** Sensitive information can be sent as the returned results since the requests come from within the same application.
  18. setResultCode(Activity.RESULT_OK);
  19. setResultData("Sensitive Info from Receiver");
  20. abortBroadcast();
  21. }
  22. }

向私有广播接收器发送广播的代码展示在下面:

要点(发送广播):

  1. 使用带有指定类的显式意图,来调用相同应用中的接收器。

  2. 敏感信息可以发送,因为目标接收器在相同应用中。

  3. 小心并安全地处理收到的返回结果,即使数据来自相同应用中的接收器。

PrivateSenderActivity.java

  1. package org.jssec.android.broadcast.privatereceiver;
  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 PrivateSenderActivity extends Activity {
  10. public void onSendNormalClick(View view) {
  11. // *** POINT 4 *** Use the explicit Intent with class specified to call a receiver within the same application.
  12. Intent intent = new Intent(this, PrivateReceiver.class);
  13. // *** POINT 5 *** Sensitive information can be sent since the destination Receiver is within the same application.
  14. intent.putExtra("PARAM", "Sensitive Info from Sender");
  15. sendBroadcast(intent);
  16. }
  17. public void onSendOrderedClick(View view) {
  18. // *** POINT 4 *** Use the explicit Intent with class specified to call a receiver within the same application.
  19. Intent intent = new Intent(this, PrivateReceiver.class);
  20. // *** POINT 5 *** Sensitive information can be sent since the destination Receiver is within the same application.
  21. intent.putExtra("PARAM", "Sensitive Info from Sender");
  22. sendOrderedBroadcast(intent, null, mResultReceiver, null, 0, null, null);
  23. }
  24. private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
  25. @Override
  26. public void onReceive(Context context, Intent intent) {
  27. // *** POINT 6 *** Handle the received result data carefully and securely,
  28. // even though the data came from the Receiver within the same application.
  29. // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."
  30. String data = getResultData();
  31. PrivateSenderActivity.this.logLine(
  32. String.format("Received result: ¥"%s¥"", data));
  33. }
  34. };
  35. private TextView mLogView;
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.main);
  40. mLogView = (TextView)findViewById(R.id.logview);
  41. }
  42. private void logLine(String line) {
  43. mLogView.append(line);
  44. mLogView.append("¥n");
  45. }
  46. }