您的位置:首页 > 移动开发 > Android开发

IntentService的使用

2016-07-18 08:35 477 查看

IntentService的使用

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This “work queue processor” pattern is commonly used to offload tasks from an application’s main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread – they may take as long as necessary (and will not block the application’s main loop), but only one request will be processed at a time.

IntentService中有6种公有的方法(Public Methods)

1.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.

除非是需要绑定服务,否则不需要使用这个方法,应为默认返回NULL

2.void onCreat():

Called by the system when the service is first created.

当服务第一次创建的时候会被系统唤醒

3.void onDestroy():

Called by the system to notify a Service that it is no longer used and is being removed.

当服务不再需要被使用的时候,系统会通知服务(notify),然后服务将会被移除(销毁)。

4.void onStart(Intent intent, int startId):

This method is deprecated. Implement onStartCommand(Intent, int, int) instead

这种方法已经被不推荐使用了,都是使用onStartCommand(Intent, int, int)来代替这种方法的。

不推荐使用的原因(源码):

@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) {
// 这里,onStartCommand方法调用了onStart方法,所以直接使用onStartCommand代替使用onStart更佳!
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}


5.int onStartCommand(Intent intent, int flags, int startId)

You should not override this method for your IntentService.

你不能使用重载这个方式。

6.void setIntentRedelivery(boolean enabled)

Sets intent redelivery preferences.

PS:

—public void setIntentRedelivery (boolean enabled)

Added in API level 5Sets intent redelivery preferences. Usually called from the constructor with your preferred semantics.

If enabled is true, onStartCommand(Intent, int, int) will return START_REDELIVER_INTENT, so if this process dies before onHandleIntent(Intent) returns, the process will be restarted and the intent redelivered. If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered.

If enabled is false (the default), onStartCommand(Intent, int, int) will return START_NOT_STICKY, and if the process dies, the Intent dies along with it.

package com.czk.testview.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
* 时间:2016年7月18日 08:03:53
* 内容:IntentService的使用
* 内容:对于IntentService,重载方法一般都是重载onHandleIntent即可
* */
public class ExampleIntentService extends IntentService {
private static final String DOWNLODING = "文件下载....";
private static final String TAG = "ExampleIntentService";
private String INFO = "";

public ExampleIntentService() {
super("ExampleIntentService");
}

protected void onHandleIntent(Intent intent) {
try {
INFO = "MyService线程名称:" + Thread.currentThread().getId();
Toast.makeText(ExampleIntentService.this, INFO, Toast.LENGTH_SHORT)
.show();
Toast.makeText(ExampleIntentService.this, DOWNLODING,
Toast.LENGTH_SHORT).show();
Log.i(TAG, INFO);
Thread.sleep(666);
Log.i(TAG+DOWNLODING, DOWNLODING);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异步 android Service