03_Service的绑定
Start Service启动的Service,Application退出,Service也不会退出;
Bind Service启动的Service,Application退出,Service就会退出。
Start Service和Stop Service仅仅是启动服务,并不能进行通信。通过Bind Service与服务进行绑定,才能与服务通信。
package com.example.servdemo; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener, ServiceConnection { private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum;
private Intent serviceIntent;
private EchoService echoService = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); serviceIntent = new Intent(this, EchoService.class); btnStartService = (Button) findViewById(R.id.btnStartService);
btnStopService = (Button) findViewById(R.id.btnStopService);
btnBindService = (Button) findViewById(R.id.btnBindService);
btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
btnGetCurrentNum = (Button) findViewById(R.id.btnGetCurrentNum); btnStartService.setOnClickListener(this);
btnStopService.setOnClickListener(this);
btnBindService.setOnClickListener(this);
btnUnbindService.setOnClickListener(this);
btnGetCurrentNum.setOnClickListener(this);
} @Override
public void onClick(View v) { switch (v.getId()) { case R.id.btnStartService:
startService(serviceIntent);
break; case R.id.btnStopService:
stopService(serviceIntent);
break; case R.id.btnBindService:
bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
break; case R.id.btnUnbindService:
unbindService(this);
echoService = null;
break; case R.id.btnGetCurrentNum:
if (echoService != null) {
System.out.println("The Number is "+echoService.getCurrentNum());
}
break;
}
} @Override
public void onServiceConnected(ComponentName name, IBinder binder) {
System.out.println("onServiceConnected!"); echoService = ( (EchoService.EchoServiceBinder)binder ).getService();
} @Override
public void onServiceDisconnected(ComponentName name) { } }
package com.example.servdemo; import java.util.Timer;
import java.util.TimerTask; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class EchoService extends Service { @Override
public IBinder onBind(Intent intent) {
System.out.println("onBind!");
return echoServiceBinder;
} private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder(); public class EchoServiceBinder extends Binder{
public EchoService getService() {
return EchoService.this;
}
} public int getCurrentNum(){
return i;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("Service Start!");
startTimer();
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("Service Stop!");
stopTimer();
super.onDestroy();
} private int i = 0; public void startTimer(){
if(timer == null){
timer = new Timer();
timerTask = new TimerTask() { @Override
public void run() {
i++;
System.out.println(i);
}
}; timer.schedule(timerTask, 1000, 1000);
}
} public void stopTimer(){
if(timer != null){
timerTask.cancel();
timer.cancel(); timer = null;
timerTask = null;
}
} private Timer timer = null;
private TimerTask timerTask = null; }
03_Service的绑定的更多相关文章
- ASP.NET Core MVC/WebAPi 模型绑定探索
前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- 冒泡,setinterval,背景图的div绑定事件,匿名函数问题
1.会冒泡到兄弟元素么? $(function(){ $("#a").click(function(){alert("a")}) $("#b" ...
- Xamarin+Prism开发详解二:Xaml文件如何简单绑定Resources资源文件内容
我们知道在UWP里面有Resources文件xxx.resx,在Android里面有String.Xml文件等.那跨平台如何统一这些类别不一的资源文件以及Xaml设计文件如何绑定这些资源?应用支持多国 ...
- 数据的双向绑定 Angular JS
接触AngularJS许了,时常问自己一些问题,如果是我实现它,会在哪些方面选择跟它相同的道路,哪些方面不同.为此,记录了一些思考,给自己回顾,也供他人参考. 初步大致有以下几个方面: 数据双向绑定 ...
- Html.DropDownLis绑定数据库
效果: 方法一: View: <div class="col-md-md-4"> <div class="input-group"> & ...
- ASP.NET MVC——模型绑定
这篇文章我们来讲讲模型绑定(Model Binding),其实在初步了解ASP.NET MVC之后,大家可能都会产生一个疑问,为什么URL片段最后会转换为例如int型或者其他类型的参数呢?这里就不得不 ...
- Spring MVC初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 proper ...
随机推荐
- Java学习的第九天
1.双色球联系: 对象的创建和使用 2.静态没太明白. 3.明天学习java 的方法
- 20200722_Oracle添加表空间、用户,用户授权
--创建表空间 CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE 'D:\dev_config\OracleTableSpaces\aifu.DB ...
- P2346 四子连棋
P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...
- 万亿级KV存储架构与实践
一.KV 存储发展历程 我们第一代的分布式 KV 存储如下图左侧的架构所示,相信很多公司都经历过这个阶段.在客户端内做一致性哈希,在后端部署很多的 Memcached 实例,这样就实现了最基本的 KV ...
- mysql运维-slave_skip_errors
1 简介 mysql在主从复制过程中,由于各种的原因,从服务器可能会遇到执行BINLOG中的SQL出错的情况,在默认情况下,服务器会停止复制进程,不再进行同步,等到用户自行来处理. sla ...
- Git--gitLab远程仓库分支代码回退的两种方案
事由:作为仓库的master,一时老眼昏花,把同事说的不合并看成了合并,直接合并了. 解决方法: 一.粗鲁的代码回退--直接在远程仓库合并 1. 在gitLab远程仓库中,基于想回退的代码的节点(co ...
- 【SpringBoot】16. 如何监控springboot的健康状况
如何监控springboot的健康状况 SpringBoot1.5.19.RELEASE 一.使用Actuator检查与监控 actuaotr是spring boot项目中非常强大的一个功能,有助于对 ...
- 经典c程序100例==61--70
[程序61] 题目:打印出杨辉三角形(要求打印出10行如下图) 1.程序分析: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 2.程序源代码: main() ...
- QQ群web前端分析二——第一印象
对QQ群WEB进行前端分析 入口是 http://qun.qzone.qq.com/ 以下为第一印象,主要是从我的理解上找问题. ----------------------------------- ...
- 基于CPU版本的Caffe推理框架
最近一段时间,认真研究了一下caffe.但是,里面内容过多,集合了CPU版本和GPU版本的代码,导致阅读起来有些复杂.因此,特意对caffe代码进行了重构,搭建一个基于CPU版本的Caffe推理框架. ...