Service学习
一、采用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学习的更多相关文章
- 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的学习暂且告一段落,因为毕竟只是对它作简要了解,至于其原理什么并不打算涉及. 在这里我提供下我之前文档的整理版本: http://kuai.xunlei.com/d/YlzvAG ...
- Web Service学习笔记
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...
- Java Restful Web Service 学习指南
Restful是一种架构style,目前常说的有restful web service, resultful http.现在热搜榜的微服务,大多数会采用Restful方式. JAX-RS 作为一个Re ...
- Java Message Service学习(一)
一,背景 近期需要用到ActiveMQ接收Oozie执行作业之后的返回结果.Oozie作为消息的生产者,将消息发送给ActiveMQ,然后Client可以异步去ActiveMQ取消息. ActiveM ...
- android Service 学习总结
学习android开发已经四五个月,由于项目中职责的原因一直没有接触过Service的实际项目,今天重新学一遍Service用法. 问题: 作为四大组件,为什么需要Service? 它与Thread又 ...
- web service 学习
是什么? 是一种远程调用技术,这种技术提供一些接口,这些接口实现让客户端和服务端进行通信和数据交换,并且让通信和交换与平台和开发语言无关.也可以说是提供了许多函数.客户端调用服务端的函数. 远程调用: ...
- Web Service学习小结(概念性回忆)-希望你们会喜欢
Web Service的出现带来了很多系统工程直接相互的调用.无疑让代码的隐藏得到了好的封装. Web Service 它的主要的组成要素: SOAP:(Simple Object Access P ...
随机推荐
- 181. Flip Bits【LintCode, by java】
Description Determine the number of bits required to flip if you want to convert integer n to intege ...
- 【转】MMORPG游戏服务器技能系统设计:表格字段与技能程序框架
本文主要从一个程序员的角度阐述一下mmorpg服务器技能系统的程序框架设计,最近在做这个,就当做一个总结吧,其中某些概念可能没有解释清楚,欢迎大家拍砖讨论~ 技能其实是战斗系统的一个组成部分,战斗基本 ...
- gdb超级基础教程
GDB超级基础教程 为什么叫超级基础呢,因为我被坑了一把.... 编译选项带 -g 就可以在可执行程序中加入调试信息,然后就可以使用gdb去查看了. 使用help命令就可以看到: (gdb) help ...
- Python replace方法并不改变原字符串
直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...
- UVa 1583 - Digit Generator 解题报告 - C语言
1.题目大意 如果a加上a的各个数字之和得到b,则说a是b的生成元.给出n其中$1\le n\le 100000$,求其最小生成元,若没有解则输出0. 2.思路 使用打表的方法打出各个数字a对应的b, ...
- angularJS遇到的坑
最近在用angularjs做一些东西,由于学艺不精,对angularjs了解不够,导致经常会不小心掉进一些自己挖的坑里(⊙_⊙),在这里记下来,谨防又踩. 1.angularjs ng-show no ...
- 第一章 Windows编程基础(1~4课)
第一课:从main到WinMain 第二课:窗口和消息 第三课:MFC编程 第四课:MFC应用程序框架 概括: Win32的两种编程框架:SDK方式.MFC方式 1. SDK方式:使用WinMain入 ...
- 软工Hello World!团队第二周博客汇总
2017.10.20-2017.10.26 Scrum会议: 第一天:http://www.cnblogs.com/liusx0303/p/7704482.html 第二天:http://www.cn ...
- 20145214 《Java程序设计》第5周学习总结
20145214 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch Java中所有错误都会被包装为对象,可以尝试try执行程序并捕捉catch代表错误的对象后做一些 ...
- Apriori算法详解
一.Apriori 算法概述Apriori 算法是一种最有影响力的挖掘布尔关联规则的频繁项集的 算法,它是由Rakesh Agrawal 和RamakrishnanSkrikant 提出的.它使用一种 ...