前一篇文章介绍了Service基本的知识,如果还不太了解Service的基本知识请参考。这篇文章里要介绍Service其它方面的一些知识。
IntentService相关的知识。
一、简介
IntentService是继承并处理异步请求的一个类。客户端通过startService(Intent)调用发送请求;该服务是根据需要启动的,使用一个工作线程来处理每个意图,当任务执行完后,IntentService会自动停止。
并在适当的时候停止服务。
所有请求都在单个工作线程处理 - 它们可能需要很长的时间(并且不会阻止应用程序的主循环),但是一次只会处理一个请求。
二、使用场景
IntentService,就非常适合用来处理这个过程。
三、用法
1.新建一个类,继承自IntentService,示例如下:
public class MyIntentService extends IntentService {
public static final String TAG = "MyIntentService";
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_FOO = "com.jiusi.equiview.action.FOO";
private static final String ACTION_BAZ = "com.jiusi.equiview.action.BAZ";
// TODO: Rename parameters
private static final String EXTRA_PARAM1 = "com.jiusi.equiview.extra.PARAM1";
private static final String EXTRA_PARAM2 = "com.jiusi.equiview.extra.PARAM2";
public MyIntentService() {
super("MyIntentService");
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG,"--------onBind--------");
return super.onBind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG,"--------onCreate--------" + " Thread = "+ Thread.currentThread().getName());
}
@Override
public void onStart(@Nullable Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(TAG,"--------onStart--------");
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Log.e(TAG,"--------onStartCommand--------");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
Log.e(TAG,"--------setIntentRedelivery--------");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionFoo(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionBaz(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
//Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
String action = intent.getExtras().getString("param");
if (action.equals("Operation1")) {
Log.e(TAG,"--------Operation1--------" + "Thread = " + Thread.currentThread().getName());
}else if (action.equals("Operation2")) {
Log.e(TAG,"--------Operation2--------"+ "Thread = " + Thread.currentThread().getName());
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG,"--------onDestroy--------");
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
2.在AndroidManifest.xml配置服务
<service
android:name=".MyIntentService"
android:exported="false" />
3.调用,本示例中是在Activity中调用的
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个
//Operation 1
Intent startServiceIntent = new Intent(this,MyIntentService.class);
Bundle bundle = new Bundle();
bundle.putString("param", "Operation1");
startServiceIntent.putExtras(bundle);
startService(startServiceIntent);
//Operation 2
Intent startServiceIntent2 = new Intent(this,MyIntentService.class);
Bundle bundle2 = new Bundle();
bundle2.putString("param", "Operation2");
startServiceIntent2.putExtras(bundle2);
startService(startServiceIntent2);
}
}
然后我们运行的结果:
05-10 18:18:54.724 16551-16551/com.jiusi.equiview E/MyIntentService: --------onCreate-------- Thread = main
--------onStartCommand--------
--------onStart--------
--------onStartCommand--------
--------onStart--------
05-10 18:18:54.724 16551-16577/com.jiusi.equiview E/MyIntentService: --------Operation1--------Thread = IntentService[MyIntentService]
05-10 18:18:56.734 16551-16577/com.jiusi.equiview E/MyIntentService: --------Operation2--------Thread = IntentService[MyIntentService]
05-10 18:18:58.734 16551-16551/com.jiusi.equiview E/MyIntentService: --------onDestroy--------
从结果可以看到,onCreate方法只执行了一次,而onStartCommand和onStart方法执行了两次,并且开启了Work Thread,这就证实了之前所说的,启动多次,但IntentService的实例只有一个,这跟传统的Service是一样的。Operation1也是先于Operation2打印,并且我让两个操作间停顿了2s,最后是onDestroy销毁了IntentService。