Activity与Service数据交互:Binder、bindService的用法
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的用法的更多相关文章
- Activity与Service之间交互并播放歌曲的实现代码
Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进 MService: 复制代码代码如下: package com.tiantian.test;im ...
- Android进阶之Fragment与Activity之间的数据交互
1 为什么 因为Fragment和Activity一样是具有生命周期,不是一般的bean通过构造函数传值,会造成异常. 2 Activity把值传递给Fragment 2.1 第一种方式,也是最常用的 ...
- 通过bind实现activity与service的交互
先点bind按钮实现onCreate,在点击start给service传值(get方法) xml: <RelativeLayout xmlns:android="http://sche ...
- Android Activity切换与Activity间数据交互
在Android程序内部, startActivity借助Intent来启动一个子Activity(使用父子关系进行表述,只为表达清晰,Android中并未有父子Activity的概念).如下: In ...
- Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- 8.1.2 绑定Activity和Service
8.1.2 绑定Activity和Service 2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Andro ...
- Android学习总结(四)—— Activity和 Service进行通信
一.Activity 和 Service进行通信的基本概念 前面我们学习我生命周期里面包含了启动和停止服务的方法,虽然服务器在活动里启动,但在启动了服务之后,活动与服务基本就没有什么关系了.我们在活动 ...
- Activity与Service进行数据交互
Android启动Service有两种方法,一种是startService,一种是bindService.生命周期如下: 执行startService时,调用者如果没有stopService,Serv ...
随机推荐
- Python3基础 assert 断言 确保程序的正确运行条件
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ISSCC 2017论文导读 Session 14:A 0.62mW Ultra-Low-Power Convolutional-Neural-Network Face-Recognition Pro
A 0.62mW Ultra-Low-Power Convolutional-Neural-Network Face-Recognition Processor and a CIS Integrate ...
- 【第六章】 springboot + 事务
在实际开发中,其实很少会用到事务,一般情况下事务用的比较多的是在金钱计算方面. mybatis与spring集成后,其事务该怎么做?其实很简单,直接在上一节代码的基础上在相应的方法(通常是servic ...
- RMQ 区间最大值 最小值查询
/*RMQ 更新最小值操作 By:draymonder*/ #include <iostream> #include <cstdio> using namespace std; ...
- jquery 浏览器打印
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 论文笔记之:End-to-End Localization and Ranking for Relative Attributes
End-to-End Localization and Ranking for Relative Attributes arXiv Paper 摘要:本文提出一种 end-to-end 的属性识别方 ...
- 软件设计师真题试题&&答案
软件设计师2013上半年上午试题及答案详解 下午试题 2013下半年上午答案 试题 下午答案 试题 2014上半年上午答案 2015上半年上午答案 2016上半年上午试题
- UVa 10570 外星人聚会
https://vjudge.net/problem/UVA-10570 题意:输入1~n的排列,每次可以交换两个整数,求出最少交换次数使之变成有序的环状序列. 思路:主要的解题方法就是寻找置换环,举 ...
- JS进阶系列之this
在javascript中,this的指向是在执行上下文的创建阶段确定的,其实只要知道不同执行方式下,this的指向分别是是什么,就能很好的掌握this这个让人摸不透的东西. 一.全局执行 全局执行又分 ...
- Miller_Rabin(米勒拉宾)素数测试算法
首先需要知道两个定理: 1: 费马小定理: 假如p是素数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p). 2:二次探测定理:如果p是素数,x是小于p的正整数,且,那么要么x=1,要么x ...