首页 > 其他分享 >安卓中实现异步任务(5)——使用IntentService实现

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

时间:2022-10-23 19:00:09浏览次数:63  
标签:异步 void IntentService Intent 安卓中 import android public

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

问题背景

上篇文章大致介绍了几种安卓汇总实现异步任务的方法,讲得比较简要,有朋友问到具体的实现方式,现在开始分列几篇文章详细介绍这几种异步的具体实现。这篇讲得是基于IntentService实现,持续更新。

问题分析

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

实现demo

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

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

import androidx.annotation.Nullable;

public class MyIntentService extends IntentService {
    private static final String TAG = "MyIntentService";
    int i = 3;
    //构造方法
    public MyIntentService() {
        super("");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        while (i > 0) {
            Log.d(TAG, "onHandleIntent i : " + i);
            i--;
            try {
                // 模拟耗时任务
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".thread.IntentServiceActivity">
    <Button
        android:id="@+id/startIntentService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动IntentService"
        android:onClick="startIntentService"
        android:gravity="center_horizontal"/>


</LinearLayout>

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

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class IntentServiceActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_service);
    }

    public void startIntentService(View view) {
        //启动service
        Intent intent = new Intent(IntentServiceActivity.this, MyIntentService.class);
        startService(intent);
    }
}

(4)执行APP log如下: image.png

关键代码分析

持续更新。。。

标签:异步,void,IntentService,Intent,安卓中,import,android,public
From: https://blog.51cto.com/baorant24/5787440

相关文章

  • 七牛云异步第三方资源抓取
    七牛云的文档,这块写的不太行呀,坑了我一早上之后才搞完/**七牛云异步第三方资源抓取*$img_url文件url*@returnQiniu\Http\ResponseObject*......
  • Java异步编程CompletableFuture
    https://blog.csdn.net/zsx_xiaoxin/article/details/123898171 https://blog.csdn.net/qq_31865983/article/details/106137777?spm=1001.2101.3001.6650.9&utm_medium......
  • 异步编程6个最佳实践
    1.尽量不要编写返回值类型为void的异步方法在通常情况下,建议大家不要编写那种返回值类型为void的异步方法,因为这样做会破坏该方法的启动者与方法本身之间的约定,这套约定本......
  • .NET 异步
    异步基础所谓异步,对于计算密集型的任务,是以线程为基础的,而在具体使用中,使用线程池里面的线程还是新建独立线程,取决于具体的任务量;对于 I/O 密集型任务的异步,是以 Windo......
  • 安卓中实现异步任务(2)——使用AsyncTask实现
    安卓中实现异步任务(2)——使用AsyncTask实现问题背景上次的文章大致介绍了几种安卓汇总实现异步任务的方法,讲得比较简要,有朋友问到具体的实现方式,现在开始分列几篇文章详细......
  • 安卓中实现异步任务(3)——使用HandlerThread实现
    #安卓中实现异步任务(3)——使用HandlerThread实现问题背景上篇文章大致介绍了几种安卓汇总实现异步任务的方法,讲得比较简要,有朋友问到具体的实现方式,现在开始分列几篇文章......
  • 安卓中实现异步任务(4)——使用线程池实现
    安卓中实现异步任务(4)——使用线程池实现问题背景上篇文章大致介绍了几种安卓汇总实现异步任务的方法,讲得比较简要,有朋友问到具体的实现方式,现在开始分列几篇文章详细介绍......
  • Generator函数异步应用
    协程协程的流程:协程A执行,执行到一半就将执行权转移给协程B,协程B交还执行权,然后A恢复执行。如下所示:function*asyncJob(){...varf=yieldreadFile(fileA);......
  • devexpress中grid控件教程 多线程异步加载数据,进度条展示
    devexpress中最强大的控件,要数它的Grid了。几乎任务数据都可以展示,但今天要用它做另一个功能。假设我们开发这样一款软件:视频编辑软件。里面有个功能,提取视频中的音频。一......
  • 利用redis作为消息队列实现异步秒杀业务
    实现消费券秒杀的优化,在加入限时抢购的优惠券时,自动的将消费券的库存stock信息也加入到redis中(可设为抢购结束后过期)抢购之前在redis中进行库存是否充足(stock)、用户是否已......