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