package com.lixu.jiaohu;

 import com.lixu.jiaohu.MyAppService.Mybind;

 import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener {
private ServiceConnection sc;
private MyAppService mMyAppService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3); btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
// 建立service连接
sc = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
Mybind mMybind = (Mybind) service;
mMyAppService = mMybind.getService();// 获取MyAppService的对象
}
};
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService();
break;
case R.id.button2:
bindService();
break;
case R.id.button3:
String str = mMyAppService.getServiceValue();
Toast.makeText(this, str, 0).show();
break;
default:
break;
} } public void startService() {
mMyAppService.setServiceValue("你是:");// 设置MyAppService里面的公共变量的值
Intent intent = new Intent(this, MyAppService.class);
startService(intent);
} public void bindService() {
Intent it = new Intent(this, MyAppService.class);
bindService(it, sc, Service.BIND_AUTO_CREATE);// 建立service的bind连接
} @Override
protected void onDestroy() {
super.onDestroy();
Intent it = new Intent(this, MyAppService.class);
stopService(it);// 关闭服务
} }
 package com.lixu.jiaohu;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class MyAppService extends Service {
private String str; @Override
public void onCreate() {
Log.e("MyAppService", "onCreate开始了");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
str = str + "宝儿";
return super.onStartCommand(intent, flags, startId);
} // Activity bindService()方法开启后进入执行onBind方法
@Override
public IBinder onBind(Intent intent) { return new Mybind();
} public String getServiceValue() {
return str;
} public void setServiceValue(String str) {
this.str = str;
} public class Mybind extends Binder {
public MyAppService getService() {
return MyAppService.this; }
} @Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
} }

Activity与Service数据交互:Binder、bindService的用法的更多相关文章

  1. Activity与Service之间交互并播放歌曲的实现代码

    Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...

  2. Android进阶之Fragment与Activity之间的数据交互

    1 为什么 因为Fragment和Activity一样是具有生命周期,不是一般的bean通过构造函数传值,会造成异常. 2 Activity把值传递给Fragment 2.1 第一种方式,也是最常用的 ...

  3. 通过bind实现activity与service的交互

    先点bind按钮实现onCreate,在点击start给service传值(get方法) xml: <RelativeLayout xmlns:android="http://sche ...

  4. Android Activity切换与Activity间数据交互

    在Android程序内部, startActivity借助Intent来启动一个子Activity(使用父子关系进行表述,只为表达清晰,Android中并未有父子Activity的概念).如下: In ...

  5. Android Activity与Service的交互方式

    参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...

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

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

  7. 8.1.2 绑定Activity和Service

    8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...

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

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

  9. Activity与Service进行数据交互

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

随机推荐

  1. The destination you provided is not a full refname (i.e., starting with "refs/")

    $ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...

  2. 【Streaming】30分钟概览Spark Streaming 实时计算

    本文主要介绍四个问题: 什么是Spark Streaming实时计算? Spark实时计算原理流程是什么? Spark 2.X下一代实时计算框架Structured Streaming Spark S ...

  3. 51NOD 1081 子段求和

    1081 子段求和   给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和.   例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} ...

  4. json.dump()和json.dmups()的区别

    在python中支持json合适的数据是通过json模块实现的. 在序列化json数据的时候遇到两个形状很像的函数,dump()和dumps().主要说说他们的区别 先看看官方文档的说明:https: ...

  5. ros 编译指定包

    一.编译到devel目录在工作空间下(有devel文件夹的那个目录)执行 catkin_make --pkg 包名 如果报错,执行catkin_make -DCATKIN_WHITELIST_PACK ...

  6. jQuery双击编辑td数据

    html <td class="remark" style="width: 200px;"> {$vo.remark} </td> js ...

  7. python2 安装scrapy出现错误提示解决办法~

    首先:set STATICBUILD=true && pip install lxml 安装环境: windows7操作系统,已经正确安装python,pip. 使用pip功能安装Sc ...

  8. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  9. PHP求并集,交集,差集

    PHP求并集,交集,差集 一.总结 一句话总结:在php中如果我想要对两个数组进行如并集.交集和差集操作,我们可直接使用php自带的函数来操作如array_merge(),array_intersec ...

  10. ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点、线、圆,显示提示信息

    ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等--绘制点.线.圆,显示提示信息 /// <summary> /// 绘制界面上的点和线 ///  ...