feat(activity): 新增启动页网络检测功能,60秒倒计时后显示连接网络入口
This commit is contained in:
@@ -129,7 +129,7 @@ open class BaseActivity : AppCompatActivity() {
|
||||
binding.tvLeftTime.text = arr[1]
|
||||
}
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
val handler = Handler(Looper.getMainLooper())
|
||||
private val updateTask = object : Runnable {
|
||||
override fun run() {
|
||||
updateDateTime()
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
package com.shuwei.dish.match.ui
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
import android.view.KeyEvent
|
||||
import android.view.animation.RotateAnimation
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.shuwei.dish.match.base.BaseActivity
|
||||
@@ -11,6 +18,7 @@ import com.shuwei.dish.match.base.GlobalData
|
||||
import com.shuwei.dish.match.databinding.ActivityInitBinding
|
||||
import com.shuwei.dish.match.db.AppRepository
|
||||
import com.shuwei.dish.match.utils.AppUtil
|
||||
import com.shuwei.dish.match.utils.NetworkUtils
|
||||
import com.shuwei.dish.match.utils.SpTool
|
||||
import com.shuwei.dish.match.utils.Weigher2
|
||||
import com.shuwei.dish.match.utils.WeightUtil
|
||||
@@ -28,10 +36,13 @@ class InitActivity : BaseActivity() {
|
||||
|
||||
private lateinit var binding: ActivityInitBinding
|
||||
private lateinit var appViewModel: AppViewModel
|
||||
private var startTime = 0L // 倒计时开始时间
|
||||
private var lastNetworkCheckTime = 0L // 上次检测网络的时间
|
||||
|
||||
@SuppressLint("HardwareIds")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
addBackKeyListener()
|
||||
statusBarDarkFont(enable = true)
|
||||
// useBackground(false)
|
||||
binding = ActivityInitBinding.inflate(layoutInflater)
|
||||
@@ -45,7 +56,6 @@ class InitActivity : BaseActivity() {
|
||||
|
||||
BaseApp.canteenId = "0"
|
||||
|
||||
|
||||
initViewModel()
|
||||
|
||||
// 主设备上/dev/ttyS7,子设备是/dev/ttyS4
|
||||
@@ -59,7 +69,10 @@ class InitActivity : BaseActivity() {
|
||||
WeightUtil.init()
|
||||
WeightUtil.getWeight()
|
||||
WeightUtil.startContinuousRead()
|
||||
countDown()
|
||||
|
||||
initViews()
|
||||
|
||||
// countDown()
|
||||
}
|
||||
|
||||
private fun initViewModel() {
|
||||
@@ -136,4 +149,131 @@ class InitActivity : BaseActivity() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化控件事件
|
||||
*/
|
||||
private fun initViews() {
|
||||
// 连接网络按钮点击事件
|
||||
binding.btnConnectNetwork.setOnClickListener {
|
||||
openNetworkSettings()
|
||||
}
|
||||
|
||||
// 启动加载动画 - 持续旋转
|
||||
startLoadingAnimation()
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动加载动画 - ImageView 持续旋转
|
||||
*/
|
||||
private fun startLoadingAnimation() {
|
||||
val rotateAnimation = RotateAnimation(
|
||||
0f, 360f,
|
||||
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
|
||||
RotateAnimation.RELATIVE_TO_SELF, 0.5f
|
||||
).apply {
|
||||
duration = 1200 // 旋转周期 1.2 秒
|
||||
repeatCount = RotateAnimation.INFINITE // 无限循环
|
||||
repeatMode = RotateAnimation.RESTART
|
||||
}
|
||||
binding.ivLoading.startAnimation(rotateAnimation)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (NetworkUtils.isNetworkConnected(this)) {
|
||||
startNextPage()
|
||||
return
|
||||
}
|
||||
startCountdown()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
// 页面不可见时停止倒计时
|
||||
handler.removeCallbacksAndMessages(null)
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动 60 秒倒计时
|
||||
*/
|
||||
private fun startCountdown() {
|
||||
startTime = SystemClock.elapsedRealtime()
|
||||
lastNetworkCheckTime = 0
|
||||
// binding.llNetwork.setBackgroundColor(Color.TRANSPARENT)
|
||||
binding.llLoading.visibility = android.view.View.VISIBLE
|
||||
binding.llNetworkButton.visibility = android.view.View.GONE
|
||||
binding.tvCountdown.text = "60秒"
|
||||
scheduleCountdown()
|
||||
}
|
||||
|
||||
/**
|
||||
* 定时更新倒计时
|
||||
* 使用 SystemClock.elapsedRealtime() 确保精确计时
|
||||
*/
|
||||
private fun scheduleCountdown() {
|
||||
handler.postDelayed({
|
||||
val elapsedTime = SystemClock.elapsedRealtime() - startTime
|
||||
val remainingTime = 60 - (elapsedTime / 1000).toInt()
|
||||
val currentNetworkCheckTime = (elapsedTime / 1000).toInt()
|
||||
|
||||
// 每 10 秒检测一次网络
|
||||
if (currentNetworkCheckTime > 0 && currentNetworkCheckTime % 10 == 0 && currentNetworkCheckTime != lastNetworkCheckTime.toInt()) {
|
||||
lastNetworkCheckTime = currentNetworkCheckTime.toLong()
|
||||
checkNetworkConnection()
|
||||
}
|
||||
|
||||
// 倒计时未结束,继续更新 UI
|
||||
if (remainingTime > 0) {
|
||||
binding.tvCountdown.text = "${remainingTime}秒"
|
||||
scheduleCountdown()
|
||||
} else {
|
||||
// 倒计时结束,最后检测一次网络
|
||||
if (NetworkUtils.isNetworkConnected(this)) {
|
||||
startNextPage()
|
||||
} else {
|
||||
// 网络未连接,显示"连接网络"按钮
|
||||
showNetworkButton()
|
||||
}
|
||||
}
|
||||
}, 100) // 每 100ms 检查一次,确保精确性
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测网络连接
|
||||
*/
|
||||
private fun checkNetworkConnection() {
|
||||
if (NetworkUtils.isNetworkConnected(this)) {
|
||||
// 网络连接成功,停止倒计时并跳转到 HomeActivity
|
||||
handler.removeCallbacksAndMessages(null)
|
||||
startNextPage()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 显示"连接网络"按钮
|
||||
*/
|
||||
private fun showNetworkButton() {
|
||||
// binding.llNetwork.setBackgroundColor("#5C77F7".toColorInt())
|
||||
binding.llLoading.visibility = android.view.View.GONE
|
||||
binding.llNetworkButton.visibility = android.view.View.VISIBLE
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开系统网络设置页面
|
||||
*/
|
||||
private fun openNetworkSettings() {
|
||||
val intent = Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)
|
||||
startActivity(intent)
|
||||
}
|
||||
|
||||
private fun addBackKeyListener() {
|
||||
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
// 禁用返回键,不执行任何操作
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.shuwei.dish.match.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
|
||||
/**
|
||||
* 网络连接检测工具类
|
||||
* 提供检测设备网络连接状态的功能
|
||||
*/
|
||||
object NetworkUtils {
|
||||
|
||||
/**
|
||||
* 检测设备是否连接到网络
|
||||
* @param context 应用上下文
|
||||
* @return true 表示已连接网络,false 表示未连接
|
||||
*/
|
||||
fun isNetworkConnected(context: Context): Boolean {
|
||||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
val network = connectivityManager.activeNetwork ?: return false
|
||||
val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
|
||||
return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="#4ECDC4" />
|
||||
<corners android:radius="12dp" />
|
||||
<stroke
|
||||
android:width="2dp"
|
||||
android:color="#2BA39F" />
|
||||
</shape>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="80dp"
|
||||
android:height="80dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<!-- 网络断开的图标 -->
|
||||
<path
|
||||
android:fillColor="#FF6B6B"
|
||||
android:pathData="M1,9l2,2c4.97,-4.97 13.03,-4.97 18,0l2,-2C16.93,2.93 7.08,2.93 1,9zM5,13l2,2c2.76,-2.76 7.24,-2.76 10,0l2,-2c-4.42,-4.42 -11.58,-4.42 -16,0zM9,17h6v2h-6z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="ring"
|
||||
android:innerRadiusRatio="3"
|
||||
android:thicknessRatio="8"
|
||||
android:useLevel="false">
|
||||
<gradient
|
||||
android:type="sweep"
|
||||
android:startColor="#FF6B6B"
|
||||
android:centerColor="#4ECDC4"
|
||||
android:endColor="#FF6B6B"
|
||||
android:angle="0" />
|
||||
</shape>
|
||||
|
||||
@@ -1,51 +1,145 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:layout_width="450dp"
|
||||
android:layout_height="430dp"
|
||||
android:layout_marginStart="50dp"
|
||||
android:layout_marginTop="250dp"
|
||||
android:layout_marginEnd="50dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:src="@drawable/img_init"
|
||||
tools:ignore="ContentDescription"
|
||||
android:visibility="invisible"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/btnInit"
|
||||
android:layout_width="300dp"
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="160dp"
|
||||
android:background="@drawable/bg_init_button"
|
||||
android:gravity="center"
|
||||
android:padding="10dp"
|
||||
android:text="设备初始化"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText"
|
||||
android:visibility="invisible"/>
|
||||
android:orientation="vertical">
|
||||
|
||||
<Space
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"/>
|
||||
<ImageView
|
||||
android:layout_width="450dp"
|
||||
android:layout_height="430dp"
|
||||
android:layout_marginStart="50dp"
|
||||
android:layout_marginTop="250dp"
|
||||
android:layout_marginEnd="50dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:src="@drawable/img_init"
|
||||
tools:ignore="ContentDescription"
|
||||
android:visibility="invisible"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivQrCode"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="200dp"
|
||||
android:layout_gravity="center_horizontal|bottom"
|
||||
android:layout_marginBottom="50dp"
|
||||
tools:src="@mipmap/ic_logo1024"
|
||||
tools:ignore="ContentDescription"
|
||||
android:visibility="invisible"/>
|
||||
<TextView
|
||||
android:id="@+id/btnInit"
|
||||
android:layout_width="300dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginTop="160dp"
|
||||
android:background="@drawable/bg_init_button"
|
||||
android:gravity="center"
|
||||
android:padding="10dp"
|
||||
android:text="设备初始化"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="30sp"
|
||||
android:textStyle="bold"
|
||||
tools:ignore="HardcodedText"
|
||||
android:visibility="invisible"/>
|
||||
|
||||
</LinearLayout>
|
||||
<Space
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/ivQrCode"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="200dp"
|
||||
android:layout_gravity="center_horizontal|bottom"
|
||||
android:layout_marginBottom="50dp"
|
||||
tools:src="@mipmap/ic_logo1024"
|
||||
tools:ignore="ContentDescription"
|
||||
android:visibility="invisible"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/llNetwork"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center">
|
||||
|
||||
<!-- Loading 动画容器 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/llLoading"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:visibility="visible"
|
||||
android:layout_marginTop="260dp">
|
||||
|
||||
<!-- 加载动画 -->
|
||||
<ImageView
|
||||
android:id="@+id/ivLoading"
|
||||
android:layout_width="160dp"
|
||||
android:layout_height="160dp"
|
||||
android:src="@drawable/loading_spinner"
|
||||
android:contentDescription="加载中" />
|
||||
|
||||
<!-- 倒计时文字 -->
|
||||
<TextView
|
||||
android:id="@+id/tvCountdown"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="60秒"
|
||||
android:textSize="40sp"
|
||||
android:textColor="@color/black"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginTop="50dp" />
|
||||
|
||||
<!-- 提示文字 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="网络连接检测中,请稍后……"
|
||||
android:textSize="22sp"
|
||||
android:textColor="#555555"
|
||||
android:layout_marginTop="20dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 连接网络按钮容器 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/llNetworkButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
android:visibility="gone">
|
||||
|
||||
<!-- 网络错误图标 -->
|
||||
<ImageView
|
||||
android:layout_width="140dp"
|
||||
android:layout_height="140dp"
|
||||
android:src="@drawable/ic_network_error"
|
||||
android:contentDescription="网络连接失败"
|
||||
android:layout_marginBottom="30dp" />
|
||||
|
||||
<!-- 连接失败提示 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="网络连接失败"
|
||||
android:textSize="36sp"
|
||||
android:textColor="@color/black"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="40dp" />
|
||||
|
||||
<!-- 连接网络按钮 -->
|
||||
<Button
|
||||
android:id="@+id/btnConnectNetwork"
|
||||
android:layout_width="350dp"
|
||||
android:layout_height="70dp"
|
||||
android:text="连接网络"
|
||||
android:textSize="24sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@android:color/white"
|
||||
android:background="@drawable/btn_connect_network" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</FrameLayout>
|
||||
Reference in New Issue
Block a user