一.使用场景
如果要通知多个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. Java笔记(一)

    1. ConcurrentModificationException 在遍历容器的同时修改容器里的成员对象可能会抛出该异常 http://www.blogjava.net/EvanLiu/archiv ...

  2. (转)python 模块安装包 制作

    转自: http://testerhome.com/topics/539 用过python的同学对于python setup.py install肯定不会陌生.那么我们自己如果封装了很多的方法怎么很好 ...

  3. LPTSTR\LPCTSTR\LPWSTR\LPCWSTR 字母的意思 及 区别

    标签: 杂谈 分类: VC     char*   是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传统的C,C++的兼容.        LP的含义 ...

  4. windows编程中的数据类型

    在windows编程中,有许多奇怪的数据类型,初学者不知道这些代表什么,下面就把一些数据类型列出如下: ATOM 原子(原子表中的一个字符串的参考) BOOL 布尔变量 BOOLEAN 布尔变量 BY ...

  5. centos7刚安装需要的一些基础优化

    基本操作一:更改主机名 centos7有一个新的修改主机名的命令hostnamectl # hostnamectl set-hostname --static benjamin # vim /etc/ ...

  6. myeclipse10.7配置resin4.0.36

    Resin-4.0.35 (built Tue, 12 Feb 2013 10:05:50 PST) Copyright(c) 1998-2012 Caucho Technology.  All ri ...

  7. 天猫首页迷思之-jquery实现左侧广告牌图片轮播

    本次要实现的是天猫首页每个楼层左侧的图片轮播效果.见图: 功能点有:点击右箭头向右滑动:点击左箭头向左滑动:什么都不点自动滑动. 1.实现样式.简单分析一下大概的html结构.一个大的div里面包含两 ...

  8. JDK7集合框架源码阅读(二) LinkedList

    基于版本jdk1.7.0_80 java.util.LinkedList 代码如下 /* * Copyright (c) 1997, 2011, Oracle and/or its affiliate ...

  9. PyCharm配置gitHub远程仓储

    在一个团队里,编码不能是闭门造车,git学起来: 1. GIT的基本介绍.安装及使用教程- @廖雪峰 2. pycharm配置github远程仓储- @谢小小XH

  10. Frequency

    题目描述 Snuke loves constructing integer sequences. There are N piles of stones, numbered 1 through N. ...