From 0abd0f73cda387d26ff64a4e8d8b737d7ac5fead Mon Sep 17 00:00:00 2001 From: xiaowusky <365019403@qq.com> Date: Thu, 17 Feb 2022 16:54:16 +0800 Subject: [PATCH] =?UTF-8?q?1.CommonApplication=E6=84=9F=E7=9F=A5=E5=89=8D?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=88=87=E6=8D=A2=202.=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E3=80=81=E8=BF=9E=E6=8E=A5=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=B8=AE=E5=8A=A9=E7=B1=BB=203.=E6=96=B0=E5=A2=9E=E9=83=A8?= =?UTF-8?q?=E5=88=86kotlin=E5=B8=B8=E7=94=A8=E6=89=A9=E5=B1=95=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- buildCommon/commonLibConfig.gradle | 2 + commonLib/build.gradle | 2 + .../com/common/commonlib/CommonApplication.kt | 17 ++++- .../common/commonlib/utils/KtExtensions.kt | 41 ++++++++++++ .../common/commonlib/utils/NetworkHelper.kt | 63 +++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 commonLib/src/main/java/com/common/commonlib/utils/KtExtensions.kt create mode 100644 commonLib/src/main/java/com/common/commonlib/utils/NetworkHelper.kt diff --git a/buildCommon/commonLibConfig.gradle b/buildCommon/commonLibConfig.gradle index 09f8a83..c3b18d3 100644 --- a/buildCommon/commonLibConfig.gradle +++ b/buildCommon/commonLibConfig.gradle @@ -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}", ] /** diff --git a/commonLib/build.gradle b/commonLib/build.gradle index a3011c1..b8c21ce 100644 --- a/commonLib/build.gradle +++ b/commonLib/build.gradle @@ -65,6 +65,8 @@ dependencies { implementation rootProject.ext.dependencies.glide // glide替换为OkHttp3代理请求 implementation rootProject.ext.dependencies.gilde_integration + // lifecycle_process + implementation rootProject.ext.dependencies.lifecycle_process // glide注解 kapt rootProject.ext.dependencies.annotationProcessor } \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt index 37f7224..f21f3c4 100644 --- a/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt +++ b/commonLib/src/main/java/com/common/commonlib/CommonApplication.kt @@ -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(){ + + } } \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/utils/KtExtensions.kt b/commonLib/src/main/java/com/common/commonlib/utils/KtExtensions.kt new file mode 100644 index 0000000..8c87623 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/utils/KtExtensions.kt @@ -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 + diff --git a/commonLib/src/main/java/com/common/commonlib/utils/NetworkHelper.kt b/commonLib/src/main/java/com/common/commonlib/utils/NetworkHelper.kt new file mode 100644 index 0000000..71cf95a --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/utils/NetworkHelper.kt @@ -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) + } +} \ No newline at end of file