课程:http://www.jikexueyuan.com/course/715_3.html?ss=1

在activity和service之间通信。

首先使用的是启动服务来通信。注意是如何使用Intent取得EditText的数据并传递给service的,在service是又是如何取出数据的。

在MainActivity中

 private EditText etData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etData = (EditText)findViewById(R.id.etData); findViewById(R.id.btnStartService).setOnClickListener(this);
findViewById(R.id.btnStopService).setOnClickListener(this);
} public void onClick(View v) {
switch (v.getId()){
case R.id.btnStartService:
Intent i = new Intent(this,MyService.class);
i.putExtra("data",etData.getText().toString());
startService(i);
break;
case R.id.btnStopService:
stopService(new Intent(this,MyService.class));
break;
}
}

在MyService中,通过启动服务的onCommand来取得数据

 public int onStartCommand(Intent intent, int flags, int startId) {

        data = intent.getStringExtra("data");

        return super.onStartCommand(intent, flags, startId);
}

接下来用绑定服务来完成通信。

增加两个绑定按钮和一个数据同步按钮

case R.id.btnBindService:
bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
break;
case R.id.btnSyncData:
if(binder!=null){
binder.setData(etData.getText().toString());

在绑定服务中需完成两个函数

 @Override
public void onServiceConnected(ComponentName name, IBinder service) { binder = (MyService.Binder)service;
} @Override
public void onServiceDisconnected(ComponentName name) { }

其中的binder悄悄完成了activiy和service之间的通信(这里面的过程很微妙,好好体会)

 public IBinder onBind(Intent intent) {

        return new Binder();
} public class Binder extends android.os.Binder{ public void setData(String data){ MyService.this.data = data;
}
}

总结下启动服务和绑定服务与活动通信的不同:

启动服务是通过Intent对象来取得数据,通过startService启动onStartCommand来过多成Intent对象的传递。

绑定服务是通过binder对象中的函数来完成通信。

前面讲的是从activity中获取数据到MyService,即从UI线程(主线程)从获取数据到子线程中,那么如何总子线程中获取数据到主线程中呢?

示例中在子线程中完成数据的变化,要求将其显示在UI的TextView上。

这里用到了回调的方式。首先定交一个接口CallBack用于向UI中传递变化的数据,在activity中实现接口中的函数,取得数据,并以message的方式传递给Handler,再将其传递给TextView进行显示。

这个过程还是挺复杂的,需要细心体会。

Service(二):通信的更多相关文章

  1. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  2. Android学习总结(四)—— Activity和 Service进行通信

    一.Activity 和 Service进行通信的基本概念 前面我们学习我生命周期里面包含了启动和停止服务的方法,虽然服务器在活动里启动,但在启动了服务之后,活动与服务基本就没有什么关系了.我们在活动 ...

  3. Android中Service通信(二)——绑定Service进行通信

    一.把输入文本的数据同步到服务的实例(如何执行服务的内部代码) 绑定服务比启动服务更加方便高效,绑定服务中的直接方法调用比Intent作为载体传输更为快捷得多. 1.activity_main.xml ...

  4. service worker(二)之主页面与service worker通信

    实现一个主页面发送消息,worker搜到信息向所有的页面派发消息(当前页面除外) msg.html <!DOCTYPE html> <html lang="en" ...

  5. Android Activity与远程Service的通信学习总结

    当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...

  6. 201709012工作日记--activity与service的通信机制

    service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...

  7. Android service ( 二) 远程服务

    通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...

  8. 社会地位即服务, Status as a Service (二): 社交网络的投资回报率 (ROI)

    本文作者 Eugene Wei 是 Amazon 战略部门的第一位分析师,后来还担任过 Flipboard, Hulu, Oculus 的产品负责人. 原文标题是 Status as a Servic ...

  9. CXF实战之在Tomcat中公布Web Service(二)

    服务接口及实现类请參考WebService框架CXF实战(一) 创建Maven Web项目,在pom.xml中加入CXF和Spring Web的引用,因为CXFServlet须要Spring Web的 ...

随机推荐

  1. Google Gson用法

    the latest version is 2.8.0. If you're using Gradle, add the following line: compile 'com.google.cod ...

  2. 基于excel导入数据到ms sql server

    OLE DB (Object Linking and Embedding, Database, sometimes written as OLEDB or OLE-DB) an API designe ...

  3. 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)

    前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...

  4. 解决错误:Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

    在使用yum install的时候,偶尔会碰见这样的错误:Couldn’t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 这是因为在你的 /etc/yum ...

  5. 应用jfinal时要注意区分Db.query和Db.find

    jfinal有一个特别好的地方,sql查询的时候可以直接查record.但是要注意query和find的区别. query返回的是List<object>,find返回的才是List< ...

  6. 如何在Windows系统下隐藏文件

    隐藏后只有键入文件夹名称才可访问,如果忘记路径就找不到了 attrib +s +a +h +r e:\bak\tools 取消的方法: attrib -a -s -h -r e:\bak\tools

  7. stl空间配置器简介

    1. 符合STL标准的空间配器接口 STL是c++中使用非常广泛的一个标准库,它包含各种有用的容器.而空间配置器作为STL各种容器的背后的核心,负责容器内部内存的分配和释放.不过空间配置器可以分配的也 ...

  8. Redis学习十一:Redis的Java客户端Jedis

    一.安装JDK tar -zxvf jdk-7u67-linux-i586.tar.gz vi /etc/profile 重启一次Centos 编码验证 二.安装eclipse 三.Jedis所需要的 ...

  9. soj2012.King(有向图+蛋疼得一逼)

    Description There are n children in a country marked by integers from 1 to n. They often fight with ...

  10. Windows上安装QT4后更改MinGW的路径

    在windows上安装使用MinGW的QT4时,并不会一起安装MinGW. 在安装过程中,会让你指定已经安装的MinGW的路径. 当你使用QT4时,将使用你指定的MinGW的路径下的g++来编译构建程 ...