ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【android】结合Google开发文档讲解已废弃的-IntentService

2021-12-25 23:00:43  阅读:389  来源: 互联网

标签:Google Service IntentService 线程 import android 废弃


阅读前若是还未清楚Service服务可以看看这篇文章
第一行代码结合google文档的Service服务解析

废弃了,但之前是咋用来着

在阅读源码的时候发现这个IntentService怎么废弃了呢?

The Android framework also provides the IntentService subclass of Service that uses a worker thread to handle all of the start requests, one at a time. Using this class is not recommended for new apps as it will not work well starting with Android 8 Oreo, due to the introduction of Background execution limits. Moreover, it’s deprecated starting with Android 11.

官网对此的解释是后台执行的限制导致在11版本的时候废弃了,那么,他到底是怎么用呢?我还没开始怎么就结束了,比龙卷风还快~

诞生,是为了补短板!

Service 一般是默认是在主线程进行的,但是很多东西是需要新开一个线程进行操作的。线程完成它的任务后就得改变,这时stopSelfstopService就发挥功效,但是忘记了呢?!

所以IntentService就出现!

省事,省时

其中的onHandleIntent方法就是为了实现不用在多构造一个线程然后开开关关的,很麻烦。反正这个对象就已经继承了Service。直接用就好!

我写了一个简单的对象,看看试试

package com.chris.servicepractice;

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

import androidx.annotation.Nullable;

/**
 * 这个IntentService虽然废弃,但是它的出现主要是避免线程的冲突,
 * 有些时候在使用Service的时候启动一个new Thread ,到头没stop掉就会持续占据,浪费资源
 */
public class MyIntentService extends IntentService {
    public MyIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //这里自动启动一个线程进行操作,不会影响到主线程
        Log.d("MyIntentService","Thread String :" + Thread.currentThread().getId());//打印线程id就会发现和主线程是不一样的。
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

}

用法和Service一样得Start,bind,unbind,stop

官方挖坑

You can use JobIntentService as a replacement for IntentService that is compatible with newer versions of Android.

它说让我们去用JobIntentService去进行替代,但是ta也废弃了~
至少在现在的时间2021-12-25,的确如此!
我可没忽悠,大家伙可以自己去读读官方文档
https://developer.android.google.cn/guide/components/services?hl=en#Lifecycle

标签:Google,Service,IntentService,线程,import,android,废弃
来源: https://blog.csdn.net/weixin_44002043/article/details/122149781

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有