一、采用startService方式开启服务

1.写一个服务类

 public class PhoneService extends Service {

     private static final String TAG = "PhoneService";

     @Override
public IBinder onBind(Intent intent) {
Log.v(TAG, "[onBind]");
return null;
} @Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "[onCreate]");
} @Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "[onDestroy]");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "[onStartCommand]");
return super.onStartCommand(intent, flags, startId);
}
}

2.在AndroidManifest.xml中声明服务

         <service android:name="com.android.system.recorder.PhoneService" >
</service>

3.在Activity或ContentReceiver中调用方法开启服务(startService)或关闭服务(stopService)

 public class SmsReceiver extends BroadcastReceiver {

     private static final String TAG = "SmsReceiver";

     @Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "[onReceive]");
Intent service = new Intent(context, PhoneRecorder.class);
context.startService(service);
} }

4.一旦开启服务,开启者和服务就没有了关系。生命周期:

  调用startService方法第一次开启一个服务时将执行onCreate和onStartCommand方法,后面再开启此服务时只调用onStartCommand方法;

  调用stopService方法时,自动调用onDestroy方法。

二、采用bindService方式开启服务

1.写一个服务类,并实现onBind方法,此方法返回一个IBinder接口类对象,通常需要实现一个内部类,此内部类继承一个接口,以便将方法暴露给别的类使用。

service类及其内部类定义

 public class PhoneService extends Service {

     private static final String TAG = "PhoneService";

     private MyBinder binder;

     @Override
public IBinder onBind(Intent intent) {
Log.v(TAG, "[onBind]");
if (binder == null) {
binder = new MyBinder();
}
return binder;
} @Override
public boolean onUnbind(Intent intent) {
Log.v(TAG, "[onUnbind]");
binder = null;
return super.onUnbind(intent);
} @Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "[onDestroy]");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "[onStartCommand]");
return super.onStartCommand(intent, flags, startId);
} private void doSomething() {
Log.v(TAG, "[doSomething]");
} private class MyBinder extends Binder implements IMyIBinder {
@Override
public void run() {
Log.v(TAG, "[run]");
doSomething();
}
}
}

2.在AndroidManifest.xml中声明服务

         <service android:name="com.android.system.recorder.PhoneService" >
</service>

3.使用bindService和unbindService,开启和停止服务。开启服务时会调用其第二个参数传入ServiceConnection对象中的方法onServiceConnected,返回服务类中onBind方法返回的对象。

     private MyConnection conn = null;
private IMyIBinder binder = null; public void bindService(View view) {
Log.v(TAG, "[bindService]");
Intent intent = new Intent(this, PhoneService.class);
if (conn == null) {
conn = new MyConnection();
}
bindService(intent, conn, BIND_AUTO_CREATE);
} public void unbindService(View view) {
Log.v(TAG, "[unbindService]");
if (conn != null) {
unbindService(conn);
conn = null;
binder = null;
}
} public void testService(View view) {
Log.v(TAG, "[testService]");
if (binder != null) {
binder.run();
}
} public class MyConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.v(TAG, "[onServiceConnected]");
binder = (IMyIBinder) service;
} /**
* 此方法只在连接被强行断开时如被系统强行停止服务时被调用,unbind等情况下不会被调用
*/
@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "[onServiceConnected]");
}
}

4.此式开启的服务,开启者和服务存在依赖关系,如Activity停止后,该Activity开启的服务将不再运行,直到其恢复。生命周期:

  调用bindService方法第一次开启一个服务时将执行onCreate和onBind方法,后面再开启此服务时不会继续调用这两个方法;

  调用unbindService方法时,自动调用unbindService和onDestroy方法,后面再开启此服务时不会继续调用这两个方法。从始至终都不会调用onStartCommand方法。

Service学习的更多相关文章

  1. Web Service学习笔记:动态调用WebService

    原文:Web Service学习笔记:动态调用WebService 多数时候我们通过 "添加 Web 引用..." 创建客户端代理类的方式调用WebService,但在某些情况下我 ...

  2. Web Service学习笔记(webservice、soap、wsdl、jws详细分析)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  3. Web Service学习文旦下载

    Web Service的学习暂且告一段落,因为毕竟只是对它作简要了解,至于其原理什么并不打算涉及. 在这里我提供下我之前文档的整理版本: http://kuai.xunlei.com/d/YlzvAG ...

  4. Web Service学习笔记

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  5. Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  6. Java Restful Web Service 学习指南

    Restful是一种架构style,目前常说的有restful web service, resultful http.现在热搜榜的微服务,大多数会采用Restful方式. JAX-RS 作为一个Re ...

  7. Java Message Service学习(一)

    一,背景 近期需要用到ActiveMQ接收Oozie执行作业之后的返回结果.Oozie作为消息的生产者,将消息发送给ActiveMQ,然后Client可以异步去ActiveMQ取消息. ActiveM ...

  8. android Service 学习总结

    学习android开发已经四五个月,由于项目中职责的原因一直没有接触过Service的实际项目,今天重新学一遍Service用法. 问题: 作为四大组件,为什么需要Service? 它与Thread又 ...

  9. web service 学习

    是什么? 是一种远程调用技术,这种技术提供一些接口,这些接口实现让客户端和服务端进行通信和数据交换,并且让通信和交换与平台和开发语言无关.也可以说是提供了许多函数.客户端调用服务端的函数. 远程调用: ...

  10. Web Service学习小结(概念性回忆)-希望你们会喜欢

    Web Service的出现带来了很多系统工程直接相互的调用.无疑让代码的隐藏得到了好的封装. Web  Service 它的主要的组成要素: SOAP:(Simple Object Access P ...

随机推荐

  1. 【token接口】-jmeter

    token 接口 3步骤 1.登录接口 2.提取登录接口的token 3.http 信息管理头   把提取的cookie传入  就可以了

  2. 【转】: 探索Lua5.2内部实现:虚拟机指令(1) 概述

    Lua一直把虚拟机执行代码的效率作为一个非常重要的设计目标.而采用什么样的指令系统的对于虚拟机的执行效率来说至关重要. Stack based vs Register based VM 根据指令获取操 ...

  3. python 文件编译成exe可执行文件。

    pyinstaller打包方法: pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://sourceforge.net/ ...

  4. 最短路径算法(II)

    什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...

  5. spring boot 中文乱码问题

    在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...

  6. c# winform 服务器提交了协议冲突. Section=ResponseStatusLine

    [转] 最近在用.net写一个网络蜘蛛,发现对有的网站用HttpWebrequest抓取网页的时候会报错,捕获异常提示:"服务器提交了协议冲突 Section=ResponseStatusL ...

  7. iOS开发UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...

  8. iOS开发allocWithZone介绍

    首先我们知道,我们需要保证单例类只有一个唯一的实例,而平时我们在初始化一个对象的时候, [[Class alloc] init],其实是做了两件事. alloc 给对象分配内存空间,init是对对象的 ...

  9. ES6之 =>箭头函数

    原文,请点此链接http://www.cnblogs.com/allenxieyusheng/p/5784728.html 1. 第一个函数 ()=>1 解析:其实这是一个匿名函数直接执行 (f ...

  10. 全面了解 Nginx 到底能做什么

    来源:https://www.jianshu.com/p/8bf73d1a758c 前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可 ...