安卓中实现异步任务(5)——使用IntentService实现

问题背景

问题分析

在日常安卓开发汇总,我们在使用 Service 时如果要执行耗时任务,总会创建一个子线程来执行,而不是直接在 Service中执行。这是因为 Service 中的程序仍然运行于主线程中,当执行一项耗时操作时,很容易导致ANR错误。当需要与 UI线程进行交互时,使用 Handler 机制来进行处理。 为了简化操作,Android提供了IntentService类。IntentService是 Android中提供的后台服务类,是Service 自动实现多线程的子类。首先,我们一起来看看具体怎么使用。

实现demo

(1)新建我们的IntentService子类,在onHandleIntent中执行耗时任务,代码如下:

  1.  
    import android.app.IntentService;
  2.  
    import android.content.Intent;
  3.  
    import android.util.Log;
  4.  
     
  5.  
    import androidx.annotation.Nullable;
  6.  
     
  7.  
    public class MyIntentService extends IntentService {
  8.  
    private static final String TAG = "MyIntentService";
  9.  
    int i = 3;
  10.  
    //构造方法
  11.  
    public MyIntentService() {
  12.  
    super("");
  13.  
    }
  14.  
     
  15.  
    @Override
  16.  
    protected void onHandleIntent(@Nullable Intent intent) {
  17.  
    while (i > 0) {
  18.  
    Log.d(TAG, "onHandleIntent i : " i);
  19.  
    i--;
  20.  
    try {
  21.  
    // 模拟耗时任务
  22.  
    Thread.sleep(100);
  23.  
    } catch (InterruptedException e) {
  24.  
    e.printStackTrace();
  25.  
    }
  26.  
    }
  27.  
    }
  28.  
    @Override
  29.  
    public void onCreate() {
  30.  
    super.onCreate();
  31.  
    Log.i(TAG, "onCreate");
  32.  
    }
  33.  
    @Override
  34.  
    public void onDestroy() {
  35.  
    super.onDestroy();
  36.  
    Log.i(TAG, "onDestroy");
  37.  
    }
  38.  
    }
学新通

注意:IntentService是service的子类,定义后要记得在manifest配置文件中进行注册: <service android:name=".thread.MyIntentService"/> (2)新建activity,对应layout布局文件代码如下:

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:tools="http://schemas.android.com/tools"
  4.  
    android:layout_width="match_parent"
  5.  
    android:layout_height="match_parent"
  6.  
    tools:context=".thread.IntentServiceActivity">
  7.  
    <Button
  8.  
    android:id="@ id/startIntentService"
  9.  
    android:layout_width="match_parent"
  10.  
    android:layout_height="wrap_content"
  11.  
    android:text="启动IntentService"
  12.  
    android:onClick="startIntentService"
  13.  
    android:gravity="center_horizontal"/>
  14.  
     
  15.  
     
  16.  
    </LinearLayout>
学新通

布局比较简单,就是一个button用来启动我们的intentservice。 (3)对应activity代码如下:

  1.  
    import android.content.Intent;
  2.  
    import android.os.Bundle;
  3.  
    import android.view.View;
  4.  
     
  5.  
    public class IntentServiceActivity extends AppCompatActivity {
  6.  
    @Override
  7.  
    protected void onCreate(Bundle savedInstanceState) {
  8.  
    super.onCreate(savedInstanceState);
  9.  
    setContentView(R.layout.activity_intent_service);
  10.  
    }
  11.  
     
  12.  
    public void startIntentService(View view) {
  13.  
    //启动service
  14.  
    Intent intent = new Intent(IntentServiceActivity.this, MyIntentService.class);
  15.  
    startService(intent);
  16.  
    }
  17.  
    }
学新通

(4)执行APP log如下: 学新通

关键代码分析

持续更新。。。