一、采用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. git 跟踪分支 远程跟踪分支 学习笔记

    远程跟踪分支相当于一个只读仓库指针,从服务器上获取数据,不可以被本地直接修改. 跟踪分支相当于一个本地指针   用于项目更新和迭代. 1跟踪分支  (tracking  branch)   逻辑示意图 ...

  2. python计算工资个税

    # -*- coding: utf-8 -*- total = int(input("税前总计:")) #公积金10% Gongjijin = total * 0.1 print( ...

  3. Python的string模块化方法

    Python 2.X中曾经存在过一个string模块,这个模块里面有很多操作字符串的方法,但是在Python 3.X中,这些模块化方法已经被移除了(但是string模块本身没有被移除,因为它还有其他可 ...

  4. 2017秋-软件工程第四次作业(2)-结对使用TDD框架完成单元测试

    第一次接触“单元测试”这个要求,我和队友学习了一些示例后开始操作.如下展示一些建立单元测试的过程.Step1:右键单击[解决方案]->左键单击[添加(D)]->[新建项目(N)]. Ste ...

  5. Notes of the scrum meeting(12.12)

    meeting time:19:30~20:30p.m.,December 12th,2013 meeting place:3号公寓一层 attendees: 顾育豪                  ...

  6. Python 再谈字符串

    字符串除了要用引号来创建之外,其他和元组一样,不能修改,如果要修改只能用切片或者拼接的方式. 其他的什么乱七八糟的运算符都一样 一些不同 capitalize()-将字符串的第一个字母大写 str1. ...

  7. ACM 第八天

    数据结构和算法目录表 数据结构和算法目录表   C C++ Java 线性结构 1. 数组.单链表和双链表 2. Linux内核中双向链表的经典实现  数组.单链表和双链表  数组.单链表和双链表   ...

  8. ZOJ 1403 F-Safecracker

    https://vjudge.net/contest/67836#problem/F "The item is locked in a Klein safe behind a paintin ...

  9. solr 学习之数据导入

    将数据库中的数据导入到我们的solr索引库中(DataImportHandler) 1.将jdbc的jar包和solr包中的DataImport的jar包拷贝到webapp中solr/WEB-INF/ ...

  10. 【转】Jsp自定义标签详解

    一.前言 原本是打算研究EXtremeComponents这个jsp标签插件,因为这个是自定义的标签,且自身对jsp的自定义标签并不是非常熟悉,所以就打算继续进行扫盲,开始学习并且整理Jsp自定义标签 ...