【学习】Service与Activity之间通信的几种方式
2016-01-05 10:43
411 查看
原文地址
通过broadcast(广播)的形式
当我们的进度发生变化的时候我们发送一条广播,然后在Activity的注册广播接收器,接收到广播之后更新ProgressBar,代码如下
总结:
Activity调用bindService (Intent service, ServiceConnection conn, int flags)方法,得到Service对象的一个引用,这样Activity可以直接调用到Service中的方法,如果要主动通知Activity,我们可以利用回调方法
Service向Activity发送消息,可以使用广播,当然Activity要注册相应的接收器。比如Service要向多个Activity发送同样的消息的话,用这种方法就更好
通过broadcast(广播)的形式
当我们的进度发生变化的时候我们发送一条广播,然后在Activity的注册广播接收器,接收到广播之后更新ProgressBar,代码如下
package com.example.communication; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends Activity { private ProgressBar mProgressBar; private Intent mIntent; private MsgReceiver msgReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //动态注册广播接收器 msgReceiver = new MsgReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.example.communication.RECEIVER"); registerReceiver(msgReceiver, intentFilter); mProgressBar = (ProgressBar) findViewById(R.id.progressBar1); Button mButton = (Button) findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //启动服务 mIntent = new Intent("com.example.communication.MSG_ACTION"); startService(mIntent); } }); } @Override protected void onDestroy() { //停止服务 stopService(mIntent); //注销广播 unregisterReceiver(msgReceiver); super.onDestroy(); } /** * 广播接收器 * @author len * */ public class MsgReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { //拿到进度,更新UI int progress = intent.getIntExtra("progress", 0); mProgressBar.setProgress(progress); } } }
package com.example.communication; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MsgService extends Service { /** * 进度条的最大值 */ public static final int MAX_PROGRESS = 100; /** * 进度条的进度值 */ private int progress = 0; private Intent intent = new Intent("com.example.communication.RECEIVER"); /** * 模拟下载任务,每秒钟更新一次 */ public void startDownLoad(){ new Thread(new Runnable() { @Override public void run() { while(progress < MAX_PROGRESS){ progress += 5; //发送Action为com.example.communication.RECEIVER的广播 intent.putExtra("progress", progress); sendBroadcast(intent); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { startDownLoad(); return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent intent) { return null; } }
总结:
Activity调用bindService (Intent service, ServiceConnection conn, int flags)方法,得到Service对象的一个引用,这样Activity可以直接调用到Service中的方法,如果要主动通知Activity,我们可以利用回调方法
Service向Activity发送消息,可以使用广播,当然Activity要注册相应的接收器。比如Service要向多个Activity发送同样的消息的话,用这种方法就更好
相关文章推荐
- 打开UltraEdit,提示 “文件可能不是DOS格式” .
- Reachability实时准确监听网络状态
- DOS批处理延时技术
- 易普优APS高级计划排程系统系列提纲:行业知识,业务建模,排程算法,计划可视化,平台框架,案例分享
- chrome调试创建sq设备进行调试
- AngularJS 使用$sce控制代码安全检查
- 解决CSS3动画首次渲染或结束时会闪烁的问题
- XML--xpath解析,自制词典
- mybatis报错nested exception is org.apache.ibatis.binding.BindingException: Parameter 'idList' not foun
- 正则表达式30分钟入门教程
- [JWFD开源工作流]AI函数必须先于DCS模块完成
- 算法(DP):矩阵链相乘
- 怎么挂载android system.img
- CC2538之TinyOS例程实验:7-blip实验 开始zigbee通信
- Intent 跳转分享数据。
- Spring中bean的生命周期
- 物理CPU、物理核跟逻辑核的区分
- Spring + iBATIS完整示例
- windows常用命令记录
- Java 单例模式详解