一、注意事项
1、设置exported属性为false。
2、确认收到的intent的正确性。
3、敏感信息只可以在同一程序中发送
二、原代码说明
1.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>@b@<manifest xmlns:android="http://schemas.android.com/apk/res/android"@b@ package="org.jssec.android.broadcast.privatereceiver" >@b@ @b@ <application@b@ android:icon="@drawable/ic_launcher"@b@ android:label="@string/app_name"@b@ android:allowBackup="false" >@b@ @b@ <!-- Private Broadcast Receiver -->@b@ <!-- *** POINT 1 *** Explicitly set the exported attribute to false. -->@b@ <receiver@b@ android:name=".PrivateReceiver"@b@ android:exported="false" />@b@ @b@ <activity@b@ android:name=".PrivateSenderActivity"@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@ </application>@b@</manifest>
2.PrivateReceiver.java
package org.jssec.android.broadcast.privatereceiver;@b@ @b@import android.app.Activity;@b@import android.content.BroadcastReceiver;@b@import android.content.Context;@b@import android.content.Intent;@b@import android.widget.Toast;@b@ @b@public class PrivateReceiver extends BroadcastReceiver {@b@ @b@ @Override@b@ public void onReceive(Context context, Intent intent) {@b@ // *** POINT 2 *** Handle the received intent carefully and securely,@b@ // even though the intent was sent from within 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(context,@b@ String.format("Received param: ¥"%s¥"", param),@b@ Toast.LENGTH_SHORT).show();@b@ @b@ // *** POINT 3 *** Sensitive information can be sent as the returned results since the requests come from with in the same application.@b@ setResultCode(Activity.RESULT_OK);@b@ setResultData("Sensitive Info from Receiver");@b@ abortBroadcast();@b@ }@b@}
三、安全代码示例
1、在同一应用内使用显示intent类用来调用receiver。@b@2、第三信息可以发送到同一应用的目标receiver中。@b@3、处理接收到的数据,确认真实性和有效性。
package org.jssec.android.broadcast.privatereceiver;@b@ @b@import android.app.Activity;@b@import android.content.BroadcastReceiver;@b@import android.content.Context;@b@import android.content.Intent;@b@import android.os.Bundle;@b@import android.view.View;@b@import android.widget.TextView;@b@ @b@public class PrivateSenderActivity extends Activity {@b@ public void onSendNormalClick(View view) {@b@ // *** POINT 4 *** Use the explicit Intent with class specified to call a receiver within the same application.@b@ Intent intent = new Intent(this, PrivateReceiver.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination Receiver is within the same applica tion.@b@ intent.putExtra("PARAM", "Sensitive Info from Sender");@b@ sendBroadcast(intent);@b@ }@b@ @b@ public void onSendOrderedClick(View view) {@b@ // *** POINT 4 *** Use the explicit Intent with class specified to call a receiver within the same application.@b@ Intent intent = new Intent(this, PrivateReceiver.class);@b@ @b@ // *** POINT 5 *** Sensitive information can be sent since the destination Receiver is within the same applica tion.@b@ intent.putExtra("PARAM", "Sensitive Info from Sender");@b@ sendOrderedBroadcast(intent, null, mResultReceiver, null, 0, null, null);@b@ }@b@ @b@ private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {@b@ @Override@b@ public void onReceive(Context context, Intent intent) {@b@ @b@ // *** POINT 6 *** Handle the received result data carefully and securely,@b@ // even though the data came from the Receiver within the same application.@b@ // Omitted, since this is a sample. Please refer to "3.2 Handling Input Data Carefully and Securely."@b@ String data = getResultData();@b@ PrivateSenderActivity.this.logLine(@b@ String.format("Received result: ¥"%s¥"", data));@b@ }@b@ };@b@ @b@ private TextView mLogView;@b@ @b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ setContentView(R.layout.main);@b@ mLogView = (TextView)findViewById(R.id.logview);@b@ }@b@ @b@ private void logLine(String line) {@b@ mLogView.append(line);@b@ mLogView.append("¥n");@b@ }@b@}