Android中如何做到自定义的广播只能有指定的app接收。
题目:Android中如何做到自定义的广播只能有指定的app接收
思路:其实我当时第一次想到的答案应该是给广播添加访问权限,但是我之前弄过给Activity的访问添加过权限,所以这里不确定对不对,但是我感觉Android中的四大组件应该一样吧。擦,结果被我蒙对了。哈哈~~
我们在使用Android中的API形式,或者使用adb命令都可以在外部打开一个应用的Activity/Service,以及发送一个广播。所以这个对于一个应用来说是很不安全的。即权限是一种保护机制。
解决思路:
其实没啥技术可言的,就是Android中可以自定义权限的,对于四大组件的访问加上一层保护,不多说了,直接上代码:
发送广播:
[java] view plaincopyprint?
- package com.tt.test;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class Main extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent i = new Intent(“COM.MESSAGE”);
- i.addCategory(“receiver”);
- i.putExtra(“message”, “haha”);
- sendOrderedBroadcast(i, “xvtian.gai.receiver”);
- }
- });
- }
- }
package com.tt.test; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("COM.MESSAGE"); i.addCategory("receiver"); i.putExtra("message", "haha"); sendOrderedBroadcast(i, "xvtian.gai.receiver"); } }); } }
AndroidManifest.xml:
[html] view plaincopyprint?
- <uses-permission android:name=”xvtian.gai.receiver” ></uses-permission>
- <permission android:protectionLevel=”normal” android:name=”xvtian.gai.receiver”></permission>
<uses-permission android:name="xvtian.gai.receiver" ></uses-permission> <permission android:protectionLevel="normal" android:name="xvtian.gai.receiver"></permission>
接收广播:
[java] view plaincopyprint?
- package com.tt.receiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class Receiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.d(“TAG”, “receiver intent:” + intent.toString());
- }
- }
package com.tt.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class Receiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("TAG", "receiver intent:" + intent.toString()); } }
AndroidManifest.xml
[html] view plaincopyprint?
- <uses-permission android:name=”xvtian.gai.receiver” ></uses-permission>
<uses-permission android:name="xvtian.gai.receiver" ></uses-permission>
[html] view plaincopyprint?
- <receiver android:name=”.Receiver” android:permission=”xvtian.gai.receivers”>
- <intent-filter>
- <action android:name=”COM.MESSAGE” />
- <category android:name=”receiver” />
- </intent-filter>
- </receiver>
<receiver android:name=".Receiver" android:permission="xvtian.gai.receivers"> <intent-filter> <action android:name="COM.MESSAGE" /> <category android:name="receiver" /> </intent-filter> </receiver>
简单吧,没任何技术可言,同理我们可以对其他的组件进行权限保护(可能用的比较多的是对Activity访问添加权限),当然我一般开发的应用中貌似不会用到这些东东,但是如果想学习的话,应该去找那些做安全的应用,比如所360手机卫士,反编译之后打开他的AndroidManifest.xml文件,你会震惊的,同时你也可以学到很多。哈哈~~
Demo下载地址:
http://download.csdn.net/detail/jiangwei0910410003/7973867