使用本地广播
来源:互联网 发布:warframe淘宝领冰队 编辑:程序博客网 时间:2024/05/17 01:08
系统的全局广播可以被其他的应用程序接收到,这样容易引入安全性问题,所以只能在应用程序本地使用的广播可以成功地解决广播安全性的问题。
我们使用LocalBroadcastManager作为广播控制器,来使用它的registerReceiver()方法来注册广播接收器,使用它的sendBroadcast()方法来发送广播,从而实现本地广播的应用。
源码:
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private localBroadcastReceiver localBroadcastReceiver;
private LocalBroadcastManager localBroadcastManager;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
localBroadcastManager=LocalBroadcastManager.getInstance(this);
button= (Button) findViewById(R.id.button);
button.setOnClickListener(this);
//获取localBroadcastReceiver的实例;
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction(“xiaowang”);
localBroadcastReceiver= new localBroadcastReceiver();
localBroadcastManager.registerReceiver(localBroadcastReceiver,intentFilter);
}
@Overrideprotected void onDestroy() { super.onDestroy(); localBroadcastManager.unregisterReceiver(localBroadcastReceiver);}@Overridepublic void onClick(View v) { Intent intent=new Intent("xiaowang"); localBroadcastManager.sendBroadcast(intent);}
}
//localBroadcastReceiver类
public class localBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,”receiver”,Toast.LENGTH_LONG).show();
}
}
- Android使用本地广播
- 使用本地广播
- 本地广播的使用
- 使用本地广播
- 使用本地广播
- 使用本地广播
- 使用本地广播
- 使用本地广播 LocalBroadcastReceiver
- Android_使用本地广播
- Android 使用本地广播
- 强制下线 使用本地广播
- 本地广播LocalBroadcastManager的使用
- 本地广播LocalBroadcastManager简单使用
- android本地广播的使用
- 第一行代码-5.4 使用本地广播
- Android开发之使用本地广播
- Android(安卓)使用本地广播
- Android学习之本地广播的使用
- iOS中text的基本知识
- 问号表达式 a?b:c
- jfreechart数据报表
- Android+JNI+OpenGL开发自己的美图秀秀
- 蓝桥杯 剪格子
- 使用本地广播
- php判断0,null,false,''类似这些值时候必须用全等
- Spring 各版本下载地址
- linux系统下ubuntu重启apache服务命令
- 什么是栈cookie保护?
- Enarm的预知未来
- xiuga
- java模拟UAS发包
- 一个人的心,最不会说谎