一、采用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. java基础-Comparator接口与Collections实现排序算法

    java 排序Comparable和Comparator使用 java提供了两个排序用的接口Comparable和Comparator,一般情况下使用区别如下: Comparable 接口用于类的固定 ...

  2. 最小生成树(II)与Kruskal算法

    为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...

  3. kvm虚拟化操作

    本节演示如何使用 virt-manager 启动 KVM 虚机. 首先通过命令 virt-manager 启动图形界面 # virt-manager 点上面的图标创建虚机 给虚机命名为 kvm1,这里 ...

  4. 【转】再谈PHP、Python与Ruby

    原文链接:http://www.nowamagic.net/librarys/veda/detail/2504 一句话总结 简单地总结: 假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成 ...

  5. Python中的相对导入语法

    Python中支持相对导入语法,即可以相对于某一个package进行导入,具体语法如下: # 导入"./dir2/spam.py", .表示当前目录 from .dir2 impo ...

  6. MFC动态创建控件及其消息响应函数

    这几天专门调研了一下MFC中如何动态创建控件及其消息响应函数. 参考帖子如下: (1)http://topic.csdn.net/u/20101204/13/5f1b1e70-2f1c-4205-ba ...

  7. lol人物模型提取(一)

      前段时间去青岛搞团建去了,闲来无事逛了会儿淘宝,无想买个lol手办,意之间发现了这张店铺宣传图:   哎呀我去,这模型做得挺逼真啊,然而这家店铺是卖zoe的cosplay道具的,不是手办-_-|| ...

  8. 微信小程序wx.pageScrollTo的替代方案

    wx.pageScrollTo这个微信小程序的api功能如下: 简而言之就是实现页面滚动的.但是在实际应用当中显得有些鸡肋,为啥呢?使用中有明显页面有明显的抖动,这无疑是极不好的用户体验.我用的华为6 ...

  9. ServiceMessage

    <?php class ServiceMessage { private $errorCode = array( '1000' => "系统错误", '1001' =& ...

  10. LINUX硬件查看命令

    1.查看系统PCI设备 lspci lspci -v   显示更详细的PCI设备信息 2.查看CPU信息 more / proc /cpuinfo 3.查看系统内存信息 more /proc /mem ...