IntentService是继承于Service和处理异步请求的一个类。
在IntentService内有一个工作线程来处理耗时操作,启动IntentService和传统的Service一样,但是当任务执行完成后,IntentService会自动停止,不需要手动停止。IntentService可以启动多次,并且每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,每次只会执行一个工作线程,执行完第一个再执行第二个,等等。
下面模拟IntentService的应用:
写一个类继承IntentService类:
public class MyIntentService extends IntentService {
// 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 = "jiaho.example.com.hellogit.action.FOO";
private static final String ACTION_BAZ = "jiaho.example.com.hellogit.action.BAZ";
// TODO: Rename parameters
private static final String EXTRA_PARAM1 = "jiaho.example.com.hellogit.extra.PARAM1";
private static final String EXTRA_PARAM2 = "jiaho.example.com.hellogit.extra.PARAM2";
public MyIntentService() {
super("MyIntentService");
}
/**
* 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) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
Log.d("TAG","-->任务A开始执行");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TAG","-->任务A执行结束");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
Log.d("TAG","-->任务B开始执行");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TAG","-->任务B执行结束");
}
}
在Activity中调用IntentService中的方法:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//MyService.startMyService(this);
//在IntentService中执行耗时操作,并且执行多个任务
MyIntentService.startActionFoo(this,"A","A");
MyIntentService.startActionBaz(this,"B","B");
}
}
这相当于启动了两次IntentService来执行任务,但是IntentService的实例只有一个。并且这两个任务是顺序执行的,下面是输出结果:
03-14 22:14:12.790 27214-27274/? D/TAG: -->任务A开始执行
03-14 22:14:32.792 27214-27274/jiaho.example.com.hellogit D/TAG: -->任务A执行结束
03-14 22:14:32.805 27214-27274/jiaho.example.com.hellogit D/TAG: -->任务B开始执行
03-14 22:14:52.805 27214-27274/jiaho.example.com.hellogit D/TAG: -->任务B执行结束
首先是执行任务A,消耗了20s的时间,执行完任务A后立马去执行任务B。