Service学习笔记
一 什么是Service
Service作为安卓四大组件之一,拥有重要的地位。Service和Activity级别相同,只是没有界面,是运行于后台的服务。这个运行“后台”是指不可见,不是指在后台线程中,事实上四大组件都是运行在UI线程中,都不能在各自的生命周期方法中执行耗时操作或者网络请求。
二 如何使用Service
Service主要可以分为两类:Local Service、Remote Service。这里以比较常用的Local Service为例,介绍Service的两种使用方法。
(1)通过Context.startService()启动Service,通过Context.stopService()结束服务。
新建一个MyService类继承Service,重写onCreate()、onStartCommand()、onDestroy()方法,然后在MainActivity中设置两个按钮,增加其各自点击事件用于启动和停止MyService。
package com.example.haisun.myapplication3; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log; /**
* Created by HaiSun on 2015/10/10.
*/
public class MyService extends Service { @Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
Log.d("MyService","onCreate executed");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService","onStartCommand executed");
// new Thread(new Runnable() {
// @Override
// public void run() {
// //具体逻辑
// stopSelf();
// }
// }).start();
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
Log.d("MyService","onDestroy executed");
super.onDestroy();
}
}
package com.example.haisun.myapplication3; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button)findViewById(R.id.start_service);
Button stop = (Button)findViewById(R.id.stop_service);
start.setOnClickListener(this);
stop.setOnClickListener(this); } @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
Intent intent = new Intent(this,MyService.class);
startService(intent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);
break; default:
break; } } }
(2)通过Context.bindService()来绑定一个service,通过Context.unbindService()解绑。
这里只是在上面的例子上增加了一些内容即可。
1.在MyService里面新建一个内部类DownBinder继承Binder
class DownLoadBinder extends Binder {
public void startDownLoad(){
Log.d("MyService","startDownLoad executed");
}
public int getProgress(){
Log.d("MyService","getProgress executed");
return 0;
}
2.通过MyService中的onBind方法返回DownBinder的实例,供Activity绑定成功后的回调
private DownLoadBinder mBinder = new DownLoadBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
3.Activity中绑定,需要新建一个ServiceConnection对象,获得回调的Binder,进而得到DownBinder实例
private MyService.DownLoadBinder downLoadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downLoadBinder = (MyService.DownLoadBinder)service;
downLoadBinder.startDownLoad();
downLoadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
4.类似的,设置两个按钮,增加绑定和解绑的点击事件
case R.id.bind_service:
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(connection);
break;
三 Service的生命周期
下图为Google官方提供的配图

附:完整的Demo地址:https://github.com/sunhai1992/ServiceTest
Service学习笔记的更多相关文章
- Web Service学习笔记:动态调用WebService
原文:Web Service学习笔记:动态调用WebService 多数时候我们通过 "添加 Web 引用..." 创建客户端代理类的方式调用WebService,但在某些情况下我 ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Web Service学习笔记
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Linux daemon与service 学习笔记
service 常驻在内存中的进程,且可以提供一些系统或网络功能,就是服务. daemon service的提供需要进程的运行,所以实现service的程序我们称为daemon. eg: 实现 ...
- Java Web Service 学习笔记
一.服务端 1. 创建Java工程 2. 创建接口HostipalServiceInterface package ws_server; import javax.jws.WebMethod; imp ...
- Dynamic CRM 2013学习笔记(二十五)JS调用web service 实现多条记录复制(克隆)功能
前面介绍过如何克隆一条当前的记录: Dynamic CRM 2013学习笔记(十四)复制/克隆记录 , 主要是通过界面上加一个字段,单击form上的clone 按钮时,改变这个字段的值以触发插件来实现 ...
- Dynamic CRM 2013学习笔记(二十九)报表设计:reporting service 报表开发常见问题
在报表开发过程中,经常会遇到各种各样的问题,比如The report cannot be displayed. (rsProcessingAborted),一点有意义的提示都没有:再就是分页问题,经常 ...
- Android(java)学习笔记227:服务(service)之服务的生命周期 与 两种启动服务的区别
1.之前我们在Android(java)学习笔记171:Service生命周期 (2015-08-18 10:56)说明过,可以回头看看: 2.Service 的两种启动方法和区别: (1)Servi ...
随机推荐
- vs2013+MVC3.0+EasyUI的ComboBox联动使用(二)
vs2013+MVC3.0+EasyUI的ComboBox联动使用(二) 简单介绍:在vs2013(.net4.0)中使用MVC3.0对于EasyUI中ComboBox的联动使用. 载入Comb ...
- xshell-常用指令汇总 linux 常用指令
suse linux 常用命令 (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 l ...
- el表达式取值优先级
不同容器中存在同名值时,从作用范围小到大的顺序依次尝试取值:pageContext->request->session->application
- 将android程序中的数据库导出到SD卡
private void copyDBToSDcrad() { String DATABASE_NAME = "数据库文件名"; String oldPath = "da ...
- CString TCHAR互相转换
CString->TCHAR*的转化可以用函数GetBuffer() // 原型:LPTSTR GetBuffer( int nMinBufLength ); CString str(_T(&q ...
- 理解ASP.NET MVC中的ActionResult
通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类 ...
- mybatis由浅入深day01_8输出映射_8.1resultType输出类型(8.1.1输出简单类型_8.1.2输出pojo对象和pojo列表_8.1.3输出hashmap)
8 输出映射 8.1 resultType(输出类型) 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功. 如果查询出来的列名和pojo中的属性名全 ...
- 件测试专家分享III GUI自动化测试相关
GUI自动化:效率为王—脚本与数据解偶 页面对象模型的核心理念是,以页面(Web Page或者Native App Page)为单位来封装页面上的空间以及控件部分操作. 而测试用力,更确切的说是操作函 ...
- Android 读写位于SD卡上的sqlite数据库文件错误问题
09-12 15:24:33.903: W/System.err(19499): java.lang.NullPointerException: Attempt to invoke virtual m ...
- COM组件技术名称解释
GUID:全局唯一标识. CLSID 或 ProgID :唯一地表示一个组件服务程序,那么根据这些ID,就可以加载运行组件,并为客户端程序提供服务了. IID :唯一的表示接口ID. COM 组件是运 ...