一、采用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. 在几份docx文档中里查找某个值

    import docx, os def readDocx(fileName): doc = docx.Document(fileName) content = '\n'.join([para.text ...

  2. [Clr via C#读书笔记]Cp13接口

    Cp13接口 类和接口继承 接口只提供签名,不提供实现:等效于契约:凡事能使用具名接口的地方都能够使用实现了的接口. 定义接口 定义很简单,FCL也提供了大量的现成接口供使用: 继承接口 类不能多继承 ...

  3. 使用Docker部署java web项目

    在国内可能会有源下载失败问题,docker安装失败, 这里提供docker离线安装包如有需要可以进行下载 docker离线安装包下载 ##本文环境使用centos 7 进行部署. #1安装docker ...

  4. Python数据分析实战-Boston Public Schools GEO数据分析-Part1

    项目目标: Boston Public Schools Geo数据是来自于Boston地区的公共学校的数据,具体描述了学校的坐标,名字,类型等.基于此数据,我们可以学习一些基本的Python数据分析的 ...

  5. HADOOP (十一).安装hbase

    下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...

  6. puppet学习笔记

    puppet优势:容易理解.用户较多.门槛低.简单.安装配置文件较少 puppet使用Ruby语言开发,安装puppet需要安装Ruby puppet运行环境:Redhat.Centos.Window ...

  7. Hero In Maze(BFS广搜)

    Description 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人^_^.突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中.Jesse听说这个消息已经是两天以后了, ...

  8. Spark GraphX 2

    顶点:VertexRDD   边:EdgeRDD.Edge.EdgeDirection   Triplet:EdgeTriplet   存储:PartitionStrategy 通常的存储方式有两种: ...

  9. khan academy js

    Documentation Quick Jump: Shapes, Complex Shapes, Colors, Text, Transforms, Environment, Mouse, Keyb ...

  10. 实验吧密码学:RSAROLL

    原题: {920139713,19} 704796792 752211152 274704164 18414022 368270835 483295235 263072905 459788476 48 ...