IntentService简介
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
IntentService(String name)
Creates an IntentService.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
IBinder | onBind(Intent intent)
Unless you provide binding for your service, you don't need to implement this method, because the default implementation returns null.
|
||||||||||
void | onCreate()
Called by the system when the service is first created.
|
||||||||||
void | onDestroy()
Called by the system to notify a Service that it is no longer used and is being removed.
|
||||||||||
void | onStart(Intent intent, int startId)
This method is deprecated. Implement
onStartCommand(Intent, int, int) instead. |
||||||||||
int | onStartCommand(Intent intent, int flags, int startId)
You should not override this method for your IntentService.
|
||||||||||
void | setIntentRedelivery(boolean enabled)
Sets intent redelivery preferences.
If enabled is true, If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered. If enabled is false (the default), 设置为true时,onStartCommand返回START_REDELIVER_INTENT,否则返回START_NOT_STICKY
关于此的更多内容请参考《Service简介》
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract void | onHandleIntent(Intent intent)
This method is invoked on the worker thread with a request to process.
This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic.
So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call
stopSelf() .该函数用于针对Intent的不同进行不同的事务处理就可以了.执行完所一个Intent请求对象所对应的工作之后,如果没有新的Intent请求达到,
则自动停止Service;否则ServiceHandler会取得下一个Intent请求传人该函数来处理其所对应的任务。
|
package com.lenovo.robin.test;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log; public class MyIntentService extends IntentService {
final static String TAG="robin";
public MyIntentService() {
super("com.lenovo.robin.test.MyIntentService");
Log.i(TAG,this+" is constructed");
}
@Override
protected void onHandleIntent(Intent arg0) {
Log.i(TAG,"begin onHandleIntent() in "+this);
try {
Thread.sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG,"end onHandleIntent() in "+this);
}
public void onDestroy()
{
super.onDestroy();
Log.i(TAG,this+" is destroy");
}
}
Intent intent=new Intent(this,MyIntentService.class);
startService(intent);
startService(intent);
startService(intent);
<service android:name=".MyIntentService" />
package android.app; import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message; public abstract class IntentService extends Service {
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
private String mName;
private boolean mRedelivery; private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
} @Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
} public IntentService(String name) {
super();
mName = name;
} public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
} @Override
public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock. super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start(); mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
} @Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
} /**
* You should not override this method for your IntentService. Instead,
* override {@link #onHandleIntent}, which the system calls when the IntentService
* receives a start request.
* @see android.app.Service#onStartCommand
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
} @Override
public void onDestroy() {
mServiceLooper.quit();
}
@Override
public IBinder onBind(Intent intent) {
return null;
} protected abstract void onHandleIntent(Intent intent);
}
IntentService简介的更多相关文章
- Android IntentService的使用和源码分析
引言 Service服务是Android四大组件之一,在Android中有着举足重轻的作用.Service服务是工作的UI线程中,当你的应用需要下载一个文件或者播放音乐等长期处于后台工作而有没有UI界 ...
- Android 进阶16:IntentService 使用及源码解析
It's time to start living the life you've only imagined. 读完本文你将了解: IntentService 简介 IntentService 源码 ...
- 什么是IntentService?有何优点?
一.IntentService 简介 IntentService 是 Service 的子类,比普通的 Service 增加了额外的功能.先看 Service 本身存在两个问题:Service 不会专 ...
- Android 面试题--Service
1.Service 是否在 main thread 中执行, service 里面是否能执行耗时的操作?默认情况,如果没有显示的指 servic 所运行的进程, Service 和 activity ...
- Android消息队列和Looper
1. 什么是消息队列 消息队列在android中对应MessageQueue这个类,顾名思义,消息队列中存放了大量的消息(Message) 2.什么是消息 消息(Message)代表一个行为(what ...
- Andorid面试问题整理
Acitivty的四中启动模式与特点. standard:默认的启动模式 singleTop:适合那种接受通知启动的页面,比如新闻客户端之类的,可能会给你推送好几次 ,但是每次都是打开同一张页面调用o ...
- Android源码分析-消息队列和Looper
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/17361775 前言 上周对Android中的事件派发机制进行了分析,这次博主 ...
- 【Android】面试宝典
Android面试 1. 内容介绍................................................................................... ...
- Android的线程和线程池
---恢复内容开始--- 一.Android线程的形态 (一)AsyncTask解析 AysncTask简介:①.实现上封装了Thread和Handler ②.不适合进行特别耗时的后台任务 Ays ...
随机推荐
- 用CSS变形创建圆形导航
http://www.w3cplus.com/css3/building-a-circular-navigation-with-css-transforms.html 本文由陈毅根据SARA SOUE ...
- cnblogs.com的用户体验
用户体验: 作为博客园的用户,我们觉得博客园的用户体验还是很不错的.但是我们觉得主界面有些混乱,一登录进去太多东西,完全不明白怎么分的类. 评价cnblogs.com的用户体验 1.你是什么样的用户, ...
- android中常见对话框之一AlertDialog
在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...
- JS原型链原理(链表)
任何一个对象都有一个prototype的属性,在js中可以把它记为:__proto__ 当初ECMAscript的发明者为了简化这门语言,同时又保持继承的属性,于是就设计了这个链表..在数据结 ...
- How can I create an executable JAR with dependencies using Maven?
http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using ...
- Nginx反向代理讲解和配置
首先来介绍下Nginx的反向代理.代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理. 画个图我们就好理解了. 正向代理:可以想象成是路由器,我们要通过它来上网的那种.(可以说是客户端的代 ...
- 你可能不知道的 Linux 命令行网络监控工具
http://developer.51cto.com/art/201505/476651_2.htm 对任何规模的业务来说,网络监控工具都 是一个重要的功能.网络监控的目标可能千差万别.比如,监控活动 ...
- AR、美颜、机器人:计算机视觉库几乎无所不在
最近日本推出的反美颜应用Primo可能让感到不胜惶恐.其实,这样反人类的应用,你也能写出,不过必须了解的一些技术,就是计算机视觉.目前,计算机视觉库包括FastCV.OpenCV.JavaCV等. 相 ...
- 关于web.config中customErrors
<customErrors>节点用于定义一些自定义错误信息的信息.此节点有Mode和defaultRedirect两个属性,其中defaultRedirect属性是一个可选属性,表示应用程 ...
- XML 命名空间(XML Namespaces)
XML 应用程序 XML CDATA XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 X ...