IntenService的用法 4-5

 

IntentService初始化是在后台操作

 

使用场景/短任务

源码分析/ServiceHandler

生命周期/onHandlelntent

注意:

SharedPreferences初始化是在 基础类的清单里注册的

ARouter 初始化是在Application ARouter一定要有Application对象才能初始化,同理Application需要在每个app的清单里注册

       

 

1.初始化服务

G:\AndroidStudioProjects\AiVoikeApp\lib_base\src\main\java\com\example\lib_base\service\InitService.kt

package com.example.lib_base.service

import android.app.IntentService
import android.content.Intent
import com.example.lib_base.utils.L

/**
 * 作者: 13967
 * 时间: 2021/2/19 9:54
 * 描述: 初始化服务
 */
class InitService:IntentService(InitService::class.java.simpleName) {
    override fun onCreate() {
        super.onCreate()
        L.i("IntentService:初始化开始")
    }

    override fun onHandleIntent(intent: Intent?) {
        L.i("IntentService:初始化操作")

    }

    override fun onDestroy() {
        super.onDestroy()
        L.i("IntentService:初始化完成")
    }
}

2.在清单文件里注册

G:\AndroidStudioProjects\AiVoikeApp\lib_base\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lib_base">

    <application>
       <service android:name=".service.InitService"/>
    </application>

</manifest>

3.后台初始化

G:\AndroidStudioProjects\AiVoikeApp\lib_base\src\main\java\com\example\lib_base\service\InitService.kt

 

//sp储存初始化

package com.example.lib_base.service

import android.app.IntentService
import android.content.Intent
import com.example.lib_base.utils.L
import com.example.lib_base.utils.SpUtils

/**
 * 作者: 13967
 * 时间: 2021/2/19 9:54
 * 描述: 初始化服务
 */
class InitService:IntentService(InitService::class.java.simpleName) {
    override fun onCreate() {
        super.onCreate()
        L.i("IntentService:初始化开始")
    }

    override fun onHandleIntent(intent: Intent?) {
        L.i("IntentService:初始化操作")

        //sp初始化
        SpUtils.initUtils(this)

    }

    override fun onDestroy() {
        super.onDestroy()
        L.i("IntentService:初始化完成")
    }
}

//软路由初始化

软路由 一定需要Application对象

package com.example.lib_base.base
import android.app.Application
import android.content.Intent
import com.example.lib_base.helper.ARouterHelper
import com.example.lib_base.service.InitService
/**
 * 作者: 13967
 * 时间: 2021/2/8 11:12
 * 描述:
 */
class BaseApp:Application() {

    override fun onCreate() {
        super.onCreate()
L.i("Application:初始化")
        //软路由初始化
        ARouterHelper.initHelper(this)
  startService(Intent(this,InitService::class.java))
    }

}

4.单个APP应用处理

package com.example.module_app_manager.base

import com.example.lib_base.base.BaseApp

/**
 * 作者: 13967
 * 时间: 2021/2/19 10:17
 * 描述:
 */
class AppmanagerApp:BaseApp() 

在每个单独App下都做如下操作

5.测试

const val isApp =true  改为 true

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值