1.CommonApplication感知前后台切换

2.网络断开、连接监听帮助类
3.新增部分kotlin常用扩展属性和方法
master
xiaowusky 3 years ago
parent 0038b64fe4
commit 0abd0f73cd

@ -40,6 +40,7 @@ project.ext {
arouter : "1.5.2",
mmkv : "1.2.10",
okhttp_logger : "3.12.7",
lifecycle_process : "2.3.1",
]
dependencies = [
@ -69,6 +70,7 @@ project.ext {
arouter : "com.alibaba:arouter-api:${versions.arouter}",
arouter_compiler : "com.alibaba:arouter-compiler:${versions.arouter}",
mmkv : "com.tencent:mmkv-static:${versions.mmkv}",
lifecycle_process : "androidx.lifecycle:lifecycle-process:${versions.lifecycle_process}",
]
/**

@ -65,6 +65,8 @@ dependencies {
implementation rootProject.ext.dependencies.glide
// glideOkHttp3
implementation rootProject.ext.dependencies.gilde_integration
// lifecycle_process
implementation rootProject.ext.dependencies.lifecycle_process
// glide
kapt rootProject.ext.dependencies.annotationProcessor
}

@ -3,6 +3,10 @@ package com.common.commonlib
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import com.common.commonlib.utils.BaseUtils
import com.tencent.mmkv.MMKV
@ -13,9 +17,10 @@ import com.tencent.mmkv.MMKV
* @since 2021/7/28
*/
@SuppressLint("StaticFieldLeak")
open class CommonApplication : Application() {
open class CommonApplication : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
intLibs(this)
}
@ -39,4 +44,14 @@ open class CommonApplication : Application() {
return commonContext
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
open fun onAppForeground(){
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
open fun onAppBackground(){
}
}

@ -0,0 +1,41 @@
/**
* Kotlin扩展属性和扩展函数
*/
package com.common.commonlib.utils
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Resources
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
/**
* Kt扩展属性判断Activity是否存活
*/
val Activity?.isAlive: Boolean
get() = !(this?.isDestroyed ?: true || this?.isFinishing ?: true)
/**
* Boolean转Visibility
*/
fun Boolean.toVisibility() = if (this) View.VISIBLE else View.GONE
/**
* Float dp2px
*/
val Float.dp: Float
get() = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
Resources.getSystem().displayMetrics
)
/**
* Int dp2px
*/
val Int.dp: Float
get() = this.toFloat().dp

@ -0,0 +1,63 @@
package com.common.commonlib.utils
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkRequest
import android.provider.Settings
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
object NetworkHelper {
/**
* 网络状态监听
*/
inline fun observerNetworkState(activity: FragmentActivity, crossinline callback: (Boolean) -> Unit) {
val manager = activity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
if (activity.isAlive) {
activity.runOnUiThread { callback(true) }
}
}
override fun onLost(network: Network) {
super.onLost(network)
if (activity.isAlive) {
activity.runOnUiThread { callback(false) }
}
}
}
manager.requestNetwork(NetworkRequest.Builder().build(), networkCallback)
activity.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
manager.unregisterNetworkCallback(networkCallback)
}
})
}
@SuppressLint("MissingPermission")
fun isNetworkConnect(context: Context): Boolean {
val cm = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return cm.activeNetworkInfo?.isAvailable ?: false
}
fun toNetworkSetting(context: Context) {
val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
context.startActivity(intent)
}
}
Loading…
Cancel
Save