标签:报名 his 方案 ack ted 获取 tar level state
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38948863
BroadCastReceiver是Android 四大组件之中的一个,应用非常广泛。也非常简单,可是我们平时在使用的过程中忽略了一个安全问题。
别人非常easy通过反编译获取到我们应用中的广播,然后频繁的向你的App中发送广播,这个当然是我们不想看到的现象,那么怎样避免应用中注冊的广播响应其它应用发送的广播呢?在解决问题之前。我们先来了解一下怎样发送一个广播。
在Android中发送一个广播通常有两种方式:显示和隐式
显式:
Intent intent=new Intent(this,MyBroadCastReceiver.class);
this.sendBroadcast(intent);隐式:
Intent intent=new Intent("com.demo.action");
this.sendBroadcast(intent);对于显示的广播除非是别人有益攻击。一般非常少出现响应别人的广播,可是对于隐式的广播就非常easy出现上述问题。由于action非常easy是一样的,一旦是一样的就出问题了。
以下就来提出解决方式:
第一种方案:
在自己的应用中,在manifest.xml中注冊receiver的时候增加export属性,例如以下:
<receiver android:name="com.baroad.demo.MyBroadCastReceiver" android:exported="false">
<intent-filter >
<action android:name="com.demo.action"/>
</intent-filter>
</receiver>另外一种方案:
自己定义权限,在manifest.xml中增加自己定义权限,然后再响应的BroadCastReceiver中增加这个权限就可以
<permission
android:name="com.yzy.permission.STARTBROAD"
android:protectionLevel="normal"> <receiver android:name="com.baroad.demo.MyBroadCastReceiver" android:permission="com.yzy.permission.STARTBROAD">
<intent-filter >
<action android:name="com.demo.action"/>
</intent-filter>
</receiver>前面两种方案都是在接收广播的地方设置。第三种是在发送方便的地方设置,设置你的广播对哪个报名有效
Intent intent=new Intent("com.demo.action");
intent.setPackage("com.two.demo");
this.sendBroadcast(intent);使用LocalBroadcastManager来实现广播
private LocalBroadcastManager mLocalBroadcastManager; private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction("com.demo.action");
mReceiver = new MyBroadCastReceiver();
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
mLocalBroadcastManager.registerReceiver(mReceiver, filter);
} public void start(View view)
{
mLocalBroadcastManager.sendBroadcast(new Intent("com.demo.action"));
}@Override
protected void onDestroy() {
mLocalBroadcastManager.unregisterReceiver(mReceiver);
super.onDestroy();
} 标签:报名 his 方案 ack ted 获取 tar level state
原文地址:http://www.cnblogs.com/lxjshuju/p/6743250.html