一. 项目整体框架
二. 技术实现
1. 基类
1.1 BaseActivity
监听网络
- 定义在Baseactivity
/**
* 无网状态—>有网状态 的自动重连操作,子类可重写该方法
*/
open fun doReConnected() {
// NetworkChangeReceiver进行监听
LiveEventBus.get("isConnected", Boolean::class.java).observe(this) {
if (it) startHttp()
}
}
- 设置一个网络状态监听器,用于监测网络连接变化
class NetworkChangeReceiver : BroadcastReceiver() {
/**
* 缓存上一次的网络状态
*/
private var hasNetwork = mmkv.decodeBool(Constant.HAS_NETWORK_KEY, true)
override fun onReceive(context: Context, intent: Intent) {
val isConnected = NetWorkUtil.isNetworkConnected(context)
if (isConnected) {
if (!hasNetwork) {
LiveEventBus.get("isConnected").post(isConnected)
}
} else {
//同时开启WIFI和数据的时候,关闭WIFI可能会造成短时间断网,所以这里再判断一次
if (!NetWorkUtil.isNetworkConnected(context)) {
LiveEventBus.get("isConnected").post(isConnected)
}
}
hasNetwork = isConnected
}
}
- 检查网络连接工具类
fun isNetworkConnected(context: Context): Boolean {
//获取系统的连接服务(ConnectivityManager)
val manager =
context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val info = manager.activeNetworkInfo
return !(null == info || !info.isConnected)
}
设置Topbar
//设置顶部toolbar相应样式
// @RequiresApi用于在 Android 开发中指示某个方法、类或字段需要的最低 API 级别
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
open fun setTop(
title: String, subTitle: Any? = null, isBack: (() -> Unit)? = {
toolbar_left_image_back?.setImageDrawable(
ContextCompat.getDrawable(this, R.drawable.write_back)
)
toolbar_left_image_back?.setOnClickListener { onBackPressed() }
}
) {
toolbar_title?.text = title
toolbar_title?.isSelected = true
//小窗模式不计算状态栏的高度
if (!DensityUtil.isSmallWindow(this)) {
val layoutParams = toolbar?.layoutParams
layoutParams?.height =
getStatusBarHeight(this) + DensityUtil.dip2px(this, Constant.TOOLBAR_HEIGHT)
toolbar?.layoutParams = layoutParams
}
//默认显示返回按钮
//在 Kotlin 中,函数类型可以用 invoke() 方法来调用
isBack?.invoke()
//根据subtitle的数据类型来显示图片或文字
when (subTitle) {
is String -> {
toolbar_subtitle_image?.visibility = View.GONE
toolbar_subtitle?.visibility = View.VISIBLE
toolbar_subtitle?.text = subTitle
}
is Int -> {
toolbar_subtitle?.visibility = View.GONE
toolbar_subtitle_image?.visibility = View.VISIBLE
toolbar_subtitle_image?.setImageResource(subTitle)
}
else -> {
toolbar_subtitle?.visibility = View.GONE
toolbar_subtitle_image?.visibility = View.GONE
}
}
}
1.2 BaseApplication
/**
* 用于管理应用的全局状态和初始化一些第三方库
*/
class BaseApplication : Application() {
companion object {
var TAG = javaClass.simpleName
// 用户信息
var userInfo: UserInfoBody? = null
lateinit var instance: Application
@SuppressLint("StaticFieldLeak")
lateinit var mContext: Context
}
override fun onCreate() {
super.onCreate()
instance = this
mContext = applicationContext
//解决7.0版本后调用相机报错的问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val builder = StrictMode.VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
}
//初始化mmkv
val rootDir = MMKV.initialize(this)
Log.d(TAG, "mmkv_root------:${rootDir}")
lateInitSDK()
}
/**
* 非必须在主线程初始化的SDK放到子线程初始化
*/
private fun lateInitSDK() {
Thread {
//设置进程的优先级,不与主线程抢资源
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
LiveEventBus.config().lifecycleObserverAlwaysActive(false).setContext(this)
initBugly()
}.start()
}
/**
* 初始化 Bugly
*/
private fun initBugly() {
// CrashReport.initCrashReport(applicationContext, Constant.BUGLY_ID, BuildConfig.DEBUG, strategy)
// 调用 Bugly 的初始化方法来设置崩溃报告功能。
// Constant.BUGLY_ID 是 Bugly 提供的应用 ID,BuildConfig.DEBUG 可能用于区分调试和发布版本。
Bugly.init(applicationContext, Constant.BUGLY_ID, false)
}
}
1.3 BaseFragment
TODOL 24/12/10