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); }
原文:http://www.cnblogs.com/scarecrow-blog/p/4901163.html