绑定本地Service并与之通信-----之一
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BindService extends Service{
private int count;
private boolean quit;
//定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
//通过继承Binder来实现IBinder类
public class MyBinder extends Binder{
public int getCount(){
//获取Service的运行状态:count
return count;
}
}
//必须实现的方法
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
//返回IBinder对象
return binder;
}
//Service被创建时回调该方法
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is Created");
//启动一条线程、动态地修改count状态值
new Thread(){
public void run() {
while(!quit){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}.start();
}
//Service被断开连接时回调该方法
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is Unbinded");
return true;
}
//Service被关闭之前回调
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
}
绑定本地Service并与之通信-----之一的更多相关文章
- Android菜鸟的成长笔记(18)——绑定本地Service并与之通信
在上一篇中介绍了Service与Activity的区别及Service两种启动方式中的第一种启动方式startService(). 我们会发现用startService().stopService() ...
- 绑定本地Service并与之通信-----之二
import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.app.Se ...
- 绑定本地Service并与之通信
绑定Service需要调用 public boolean bindService (Intent service, ServiceConnection conn, int flags): 传入一个Se ...
- Android开发学习之路-Service和Activity的通信
在很多时候,Service都不仅仅需要在后台运行,还需要和Activity进行通信,或者接受Activity的指挥,如何来实现,来看代码. 定义一个服务 // 创建一个服务,然后在onBind()中返 ...
- Android Service、IntentService,Service和组件间通信
Service组件 Service 和Activity 一样同为Android 的四大组件之一,并且他们都有各自的生命周期,要想掌握Service 的用法,那就要了解Service 的生命周期有哪些方 ...
- 绑定本地的Session
绑定本地的Session图示解析: 代码的结构: 代码: SaveServlet.java package com.itheima.servlet; import java.io.IOExceptio ...
- C# 开源一个基于 yarp 的 API 网关 Demo,支持绑定 Kubernetes Service
关于 Neting 刚开始的时候是打算使用微软官方的 Yarp 库,实现一个 API 网关,后面发现坑比较多,弄起来比较麻烦,就放弃了.目前写完了查看 Kubernetes Service 信息.创建 ...
- 【Service Fabric】小白入门记录 本地Service Fabric集群安装及设置
本篇内容是自学自记,现在我还不知道Service Fabric究竟是怎么个入门法,反正按照入门教程先进行本地Service Fabric集群的安装,万里路始于足下,要学习总得先把环境装好了才能开始学习 ...
- socket的bind函数是不是只能绑定本地IP,不能绑定外网IP么?
参考: https://bbs.csdn.net/topics/391024376 别瞎猜测. 所谓bind,就是指绑定本地接受端口. 指定ip,是为了分辨多ip主机. --------------- ...
随机推荐
- iOS - Swift PList 数据存储
前言 直接将数据写在代码里面,不是一种合理的做法.如果数据经常改,就要经常翻开对应的代码进行修改,造成代码扩展性低.因此,可以考虑将经常变的数据放在文件中进行存储,程序启动后从文件中读取最新的数据.如 ...
- 转:HTTP 301 跳转和302跳转的区别
301和302 Http状态有啥区别?301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently ...
- 让你分分钟读懂CPU架构及芯片厂商
CPU架构是CPU厂商给属于同一系列的CPU产品定的一个规范,主要目的是为了区分 不同类型CPU的重要标示.目前市面上的CPU指令集分类主要分有两大阵营,一个是intel.AMD为首的复杂指令集CPU ...
- (一)SecureCRT连接虚拟机linux
最近在学习linux,在使用SecureCRT连接虚拟机linux时遇到了一些问题,现在总结一下. 1.首先要配置linux配置文件,修改静态IP地址以及掩码,保持与本地在同一网段.更改配置文件方法如 ...
- iOS开发之用Xcode 在真机上截屏与模拟器截屏
一.真机截屏 1.打开Xcode 6 2.在xcode 选择模拟器或者真机设备的地方选中你的真机 3.Debug-->View Debugging-->Take Screenshot of ...
- 【linux命令】grep
1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...
- 转:为什么C++中空类和空结构体大小为1?
参考:http://www.spongeliu.com/260.html 为什么C++中空类和空结构体大小为1? On November 17, 2010, in C语言, 语言学习, by spon ...
- 工作流学习——Activiti整体认识二步曲 (zhuan)
http://blog.csdn.net/zwk626542417/article/details/46594505 ***************************************** ...
- 颜色缩减(带Trackbar)【从毛星云Opencv3编程入门P75 P111例程改编】
最近学了点opencv,买了毛星云的书,大力推荐哦. 颜色缩减,自己加了个Trackbar看起来更直观一些. 我一般自己先看一遍程序,脑子里有个大概印象了,再自己写一遍,这样出了错误会印象更深刻. 1 ...
- 测序深度和覆盖度(Sequencing depth and coverage)
总是跑数据,却对数据一无所知,这说不过去吧. 看几篇文章吧 Sequencing depth and coverage: key considerations in genomic analyses( ...