一.使用场景
如果要通知多个Activity,广播较为适合.但广播较为耗费性能.

二.Broadcast更新Activity中的UI

1.新建一个接口OnUpdateUI,用于回调更新UI

public interface OnUpdateUI {
void updateUI(String i);
}

2.BroadcastReceiver代码

public class MyBroadcast extends BroadcastReceiver {

    OnUpdateUI onUpdateUI;
@Override
public void onReceive(Context context, Intent intent) {
String progress = intent.getStringExtra("progress");
onUpdateUI.updateUI(progress);
} public void SetOnUpdateUI(OnUpdateUI onUpdateUI){
this.onUpdateUI = onUpdateUI;
} }

3.Activity中代码:

public class MainActivity extends Activity {

    public static final String FLAG = "UPDATE";
MyBroadcast myBroadcast;
TextView tip;
Intent intent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tip = (TextView)findViewById(R.id.tip); myBroadcast = new MyBroadcast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(FLAG);
registerReceiver(myBroadcast, intentFilter); intent = new Intent(this, MyService.class); // 更新UI
myBroadcast.SetOnUpdateUI(new OnUpdateUI() {
@Override
public void updateUI(String i) {
tip.setText(i);
}
});
} protected void myClick(View v){
// 发送广播
if( v.getId() == R.id.btn ){
Intent intent = new Intent();
intent.setAction(FLAG);
intent.putExtra("progress", System.currentTimeMillis()+"");
sendBroadcast(intent);
} // 启动服务
if (v.getId() == R.id.btn2){
startService(intent);
}
} @Override
protected void onDestroy() {
unregisterReceiver(myBroadcast);
stopService(intent);
super.onDestroy();
}
}

三.Service发送广播,广播更新Activity中UI

Service发送广播

Intent it = new Intent();
it.setAction(MainActivity.FLAG);
it.putExtra("progress", i+"");
sendBroadcast(it);

Service和Activity交互之广播方式的更多相关文章

  1. android开发之使用Messenger实现service与activity交互

    service与activity交互的方式有多种,这里说说使用Messenger来实现两者之间的交互. Service程序 public class MessengerService extends ...

  2. Android—Service与Activity的交互

    service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...

  3. Android Service与Activity之间通信的几种方式

    在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...

  4. 本地/远程Service 和Activity 的交方式(转)

    android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Remo ...

  5. Android开发--Service和Activity通过广播传递消息

    Android的Service也运行在主线程,但是在服务里面是没法直接调用更改UI,如果需要服务传递消息给Activity,通过广播是其中的一种方法: 一.在服务里面发送广播 通过intent传送数据 ...

  6. Service与Activity通信 回调方式***

    要实现service与activity的高强度通信用什么方法? service与activity之前的通信方式有很多,回调接口方式.观察者模式.广播.还有handler等,方法有很多,但要高强度地通信 ...

  7. Activity与Service进行数据交互

    Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...

  8. (Android数据传递)Service和Activity之间-- 借助BroadcastReceiver--的数据传递

    实现逻辑如下: 左侧为Activity中的执行逻辑,右侧为Service中的执行逻辑: /** * <功能描述> Service和Activity之间的数据交互:具体表现为: 1. 从Se ...

  9. Android Service与Activity之间通信

    主要分为: 通过Binder对象 通过broadcast(广播)的形式 Activity调用bindService (Intent service, ServiceConnection conn, i ...

随机推荐

  1. bzoj1630/2023 [Usaco2007 Demo]Ant Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1630 http://www.lydsy.com/JudgeOnline/problem.ph ...

  2. 日志组件Log4Net

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...

  3. Linux Mint---fcitx中文,日语输入法

    安装中文输入法,我这边选了小企鹅,先前用过scim,稍微比较了下,效果还是这个好 安装可一步即可搞定 apt-get install fcitx fcitx-table-wubi-large fcit ...

  4. I2C总线介绍

    1. 简介 I2C, Inter-Integrated Circuit, 是一种串行通信总线,用于连接微控制器及其外围设备它是一种两线式串行总线(串行数据:SDA; 串行时钟频率:SCL), 利用电阻 ...

  5. 【dlib代码解读】人脸检测器的训练【转】

    转自:http://blog.csdn.net/elaine_bao/article/details/53046542 版权声明:本文为博主原创文章,转载请注明.   目录(?)[-] 综述 代码解读 ...

  6. Less的用法

    Less常用来写样式,比较多的用法是使用第三方软件编译成CSS文件,然后在HTML页面引入CSS文件.而不是直接在HTML页面里引入编译文件和Less文件.如此以来,在后期修改方便的多.当然,在写小项 ...

  7. oracle 批量改temp/data/redo file的路径

    批量生成修改路径的脚本.select 'alter database rename file ''' || name ||'''' || ' to '''|| substr(name,0,instr( ...

  8. maven 在 mac中的配置

    思前想后,还是在mac中把maven配置一下吧. 1.下载安装包,由于公司用的版本比较低,考虑到兼容性,建议用低版本的.我用3.0.5 下载地址:http://archive.apache.org/d ...

  9. 【转】玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo

    http://blog.csdn.net/yanzi1225627/article/details/33339965 GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而 ...

  10. HDU 6396 Swordsman --------2018 Multi-University Training Contest 7 (模拟+读入挂)

    原题地址: 打怪升级 一开始有N个怪物:主角有K个能力:只有K个能力都击败怪物才能斩杀怪物并获得K个能力的增值:问最多能杀几个怪物: 做法: 用优先队列把怪物能力装进去:能力小放前面: 最重要的是数据 ...