IntentService
2021-06-22/2021-06-22
参考文献:Android多线程(IntentService篇)
1. 前言
例如上传下载等操作原则上要尽可能的交给 Service 去做,原因就是上传等过程中用户可能会有将应用至于后台,那这时候 Activity 很有可能就被杀死了。如果担心 Service 被杀死还能通过 startForeground 提升优先级。
但在 Service 里需要开启线程才能进行耗时操作,自己管理 Service 与线程听起来就不像一个优雅的做法,此时就可以用到 Android 提供的一个类,IntentService。
2. 基础使用
- 创建 MyIntentService 继承 IntentService,并在 onHandlIntent() 中实现耗时操作
- Activity 注册并启动广播监听耗时操作完成事件,如更新 UI
- Activity 中启动 MyIntentService
- 记得 IntentServic 也是 Service 要注册
1public class MyIntentService extends IntentService {
2 private static final String ACTION_UPLOAD_IMG = "com.qit.action.UPLOAD_IMAGE";
3 public static final String EXTRA_IMG_PATH = "com.qit.extra.IMG_PATH";
4
5 // 上传图片,回调 onHandleIntent 方法。
6 public static void startUploadImg(Context context, String path) {
7 Intent intent = new Intent(context, MyIntentService.class);
8 intent.setAction(ACTION_UPLOAD_IMG);
9 intent.putExtra(EXTRA_IMG_PATH, path);
10 context.startService(intent);
11 }
12
13
14 public MyIntentService() {
15 super("MyIntentService");
16 }
17
18 @Override
19 protected void onHandleIntent(Intent intent) {
20 if (intent != null) {
21 final String action = intent.getAction();
22 if (ACTION_UPLOAD_IMG.equals(action)) {
23 final String path = intent.getStringExtra(EXTRA_IMG_PATH);
24 handleUploadImg(path);
25 }
26 }
27 }
28
29 private void handleUploadImg(String path) {
30 try {
31 //模拟上传耗时
32 Thread.sleep(((long) (Math.random() * 3000)));
33
34 Intent intent = new Intent(MainActivity.UPLOAD_RESULT);
35 intent.putExtra(EXTRA_IMG_PATH, path);
36 // 上传完成发送广播
37 sendBroadcast(intent);
38 } catch (InterruptedException e) {
39 e.printStackTrace();
40 }
41 }
42}
1public class MainActivity extends AppCompatActivity {
2 public static final String UPLOAD_RESULT = "com.qit.UPLOAD_RESULT";
3
4 private LinearLayout ll;
5
6 @Override
7 protected void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.activity_main);
10
11 ll = (LinearLayout) findViewById(R.id.ll);
12 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
13 @Override
14 public void onClick(View v) {
15 // 添加上传任务
16 addTask();
17 }
18 });
19 // 注册接收器
20 IntentFilter filter = new IntentFilter();
21 filter.addAction(UPLOAD_RESULT);
22 registerReceiver(uploadImgReceiver, filter);
23 }
24
25 // 操作完成广播接收器
26 private BroadcastReceiver uploadImgReceiver = new BroadcastReceiver() {
27 @Override
28 public void onReceive(Context context, Intent intent) {
29 if (intent.getAction() == UPLOAD_RESULT) {
30 String path = intent.getStringExtra(MyIntentService.EXTRA_IMG_PATH);
31 handleResult(path);
32 }
33
34 }
35 };
36
37 // 处理结果
38 private void handleResult(String path) {
39 TextView tv = (TextView) ll.findViewWithTag(path);
40 tv.setText(path + " upload success ~~~ ");
41 }
42
43 int i = 0;
44
45 public void addTask() {
46 //模拟路径
47 String path = "/sdcard/imgs/" + (++i) + ".png";
48 // 开启 service
49 MyIntentService.startUploadImg(this, path);
50
51 TextView tv = new TextView(this);
52 ll.addView(tv);
53 tv.setText(path + " is uploading ...");
54 tv.setTag(path);
55 }
56
57
58 @Override
59 protected void onDestroy() {
60 super.onDestroy();
61 unregisterReceiver(uploadImgReceiver);
62 }
63}