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. Python3基础 函数 可变参数,将传进来的参数转成列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. bootstrap4

    lipsum, lorem生成假文, 是在编辑器中按tab键时生成的, 那个时候就已经生成了, 所以你在浏览器上看到的内容就是编辑器中的内容, 这个内容不会再变了. 所以你不要企图想刷新浏览器而改变假 ...

  3. 获取转UTF8的字符串

    /// <summary> /// 获取转UTF8的字符串 /// </summary> /// <param name="strWord">& ...

  4. IntelliJ-IDEA和Git、GitHub、Gitlab的使用

    一.基本入门 1.IntelliJ-IDEA预装的版本控制介绍 我们来看IntelliJ-IDEA的版本控制设置区域 打开File>Settings>Version Control  可以 ...

  5. 【第十二章】 springboot + mongodb(复杂查询)

    简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(Bas ...

  6. Unity3D学习笔记(五):坐标系、向量、3D数学

    Unity复习 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ...

  7. spark监控入门

    前言 Spark作为计算引擎每天承载了大量的计算任务,为了监控集群的资源使用情况,对spark的监控也在所难免,Spark的监控有3个入口,1. Rest; 2.另一个是Metrics; 3. Log ...

  8. hdoj上的一题和程序设计第二次作业的拓展-人见人爱a+b

    hdoj上一道有意思的题目,题目: 人见人爱a+b 敲的也蛮快的,大概十分钟左右就AC了.代码如下: 人见人爱a+b #include<stdio.h> int main() { int ...

  9. iBatis的CRUD操作详细总结

    昨天晚上看了一下关于iBatis的一个讲解的视频,讲的和我的这个简单的总结差不多.... 思考了一下还是把主要操作都总结一下吧,当然这里也不是全的,知识简单的CRUD... 首先我觉得持久层的操作主要 ...

  10. URAL 1183 Brackets Sequence

    URAL 1183 思路:区间dp,打印路径,详见http://www.cnblogs.com/widsom/p/8321670.html 代码: #include<iostream> # ...