feat(activity): 新增 InitActivity 初始化页面,实现网络检测和倒计时功能
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
|
||||||
android:maxSdkVersion="28" />
|
android:maxSdkVersion="28" />
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".MyApp"
|
android:name=".MyApp"
|
||||||
@@ -40,16 +41,11 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".activity.HomeActivity"
|
android:name=".activity.HomeActivity"
|
||||||
android:configChanges="orientation|screenSize"
|
android:configChanges="orientation|screenSize"
|
||||||
android:exported="true"
|
android:exported="false"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:screenOrientation="landscape"
|
android:screenOrientation="landscape"
|
||||||
android:theme="@style/Theme.Inbound">
|
android:theme="@style/Theme.Inbound" />
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.MAIN" />
|
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
<activity android:name="com.sw.inbound.activity.ReceiptActivity"
|
<activity android:name="com.sw.inbound.activity.ReceiptActivity"
|
||||||
android:screenOrientation="landscape"
|
android:screenOrientation="landscape"
|
||||||
android:windowSoftInputMode="adjustPan" />
|
android:windowSoftInputMode="adjustPan" />
|
||||||
@@ -66,19 +62,20 @@
|
|||||||
android:name="com.sw.inbound.MainActivity"
|
android:name="com.sw.inbound.MainActivity"
|
||||||
android:screenOrientation="landscape" />
|
android:screenOrientation="landscape" />
|
||||||
|
|
||||||
<!-- <activity-->
|
<!-- 初始化页面 Activity -->
|
||||||
<!-- android:name=".InitActivity"-->
|
<activity
|
||||||
<!-- android:configChanges="orientation|screenSize"-->
|
android:name=".activity.InitActivity"
|
||||||
<!-- android:exported="true"-->
|
android:configChanges="orientation|screenSize"
|
||||||
<!-- android:label="@string/app_name"-->
|
android:exported="true"
|
||||||
<!-- android:screenOrientation="landscape"-->
|
android:label="@string/app_name"
|
||||||
<!-- android:theme="@style/Theme.Inbound">-->
|
android:screenOrientation="landscape"
|
||||||
<!-- <intent-filter>-->
|
android:theme="@style/Theme.Inbound">
|
||||||
<!-- <action android:name="android.intent.action.MAIN" />-->
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
<!-- </intent-filter>-->
|
</intent-filter>
|
||||||
<!-- </activity>-->
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -3,6 +3,7 @@ package com.sw.inbound.activity
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.view.KeyEvent
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import com.sw.inbound.base.BaseActivity
|
import com.sw.inbound.base.BaseActivity
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
@@ -16,6 +17,7 @@ import com.sw.inbound.utils.DateTimeUtils
|
|||||||
import com.sw.inbound.utils.ThreadUtils
|
import com.sw.inbound.utils.ThreadUtils
|
||||||
import com.sw.inbound.utils.ext.gone
|
import com.sw.inbound.utils.ext.gone
|
||||||
import com.sw.inbound.utils.ext.startActivity
|
import com.sw.inbound.utils.ext.startActivity
|
||||||
|
import com.sw.inbound.utils.ext.toast
|
||||||
import com.sw.inbound.utils.ext.visible
|
import com.sw.inbound.utils.ext.visible
|
||||||
import com.sw.inbound.viewmodel.UserViewModel
|
import com.sw.inbound.viewmodel.UserViewModel
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
@@ -45,6 +47,10 @@ class HomeActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 用于检测双击返回键的时间戳 */
|
||||||
|
private var lastBackPressTime = 0L
|
||||||
|
private val backPressInterval = 2000L // 2 秒内连续按下返回键视为双击
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityHomeBinding.inflate(layoutInflater)
|
binding = ActivityHomeBinding.inflate(layoutInflater)
|
||||||
@@ -133,4 +139,27 @@ class HomeActivity : BaseActivity() {
|
|||||||
// 页面不可见时停止刷新,避免内存泄漏
|
// 页面不可见时停止刷新,避免内存泄漏
|
||||||
handler.removeCallbacks(timeRunnable)
|
handler.removeCallbacks(timeRunnable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理返回键事件
|
||||||
|
* 双击返回键退出应用,单击返回键提示再次点击退出
|
||||||
|
*/
|
||||||
|
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
||||||
|
return if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||||
|
val currentTime = System.currentTimeMillis()
|
||||||
|
if (currentTime - lastBackPressTime < backPressInterval) {
|
||||||
|
// 双击返回键,退出应用
|
||||||
|
finish()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
// 单击返回键,显示提示
|
||||||
|
lastBackPressTime = currentTime
|
||||||
|
this.toast("再按一次返回键退出应用")
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
super.onKeyDown(keyCode, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package com.sw.inbound.activity
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.os.Handler
|
||||||
|
import android.os.Looper
|
||||||
|
import android.os.SystemClock
|
||||||
|
import android.view.KeyEvent
|
||||||
|
import android.view.animation.RotateAnimation
|
||||||
|
import com.sw.inbound.base.BaseActivity
|
||||||
|
import com.sw.inbound.databinding.ActivityInitBinding
|
||||||
|
import com.sw.inbound.utils.NetworkUtils
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化页面 Activity
|
||||||
|
* 用于应用启动时的初始化流程
|
||||||
|
* - 显示 60 秒倒计时
|
||||||
|
* - 每 10 秒检测一次网络连接
|
||||||
|
* - 网络连接成功则跳转到 HomeActivity
|
||||||
|
* - 60 秒后仍未连接则显示"连接网络"按钮
|
||||||
|
*/
|
||||||
|
class InitActivity : BaseActivity() {
|
||||||
|
|
||||||
|
private lateinit var binding: ActivityInitBinding
|
||||||
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
private var startTime = 0L // 倒计时开始时间
|
||||||
|
private var lastNetworkCheckTime = 0L // 上次检测网络的时间
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
binding = ActivityInitBinding.inflate(layoutInflater)
|
||||||
|
setContentView(binding.root)
|
||||||
|
initViews()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化控件事件
|
||||||
|
*/
|
||||||
|
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()
|
||||||
|
startCountdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPause() {
|
||||||
|
super.onPause()
|
||||||
|
// 页面不可见时停止倒计时
|
||||||
|
handler.removeCallbacksAndMessages(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动 60 秒倒计时
|
||||||
|
*/
|
||||||
|
private fun startCountdown() {
|
||||||
|
startTime = SystemClock.elapsedRealtime()
|
||||||
|
lastNetworkCheckTime = 0
|
||||||
|
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)) {
|
||||||
|
navigateToHome()
|
||||||
|
} else {
|
||||||
|
// 网络未连接,显示"连接网络"按钮
|
||||||
|
showNetworkButton()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 100) // 每 100ms 检查一次,确保精确性
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测网络连接
|
||||||
|
*/
|
||||||
|
private fun checkNetworkConnection() {
|
||||||
|
if (NetworkUtils.isNetworkConnected(this)) {
|
||||||
|
// 网络连接成功,停止倒计时并跳转到 HomeActivity
|
||||||
|
handler.removeCallbacksAndMessages(null)
|
||||||
|
navigateToHome()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跳转到 HomeActivity
|
||||||
|
*/
|
||||||
|
private fun navigateToHome() {
|
||||||
|
val intent = Intent(this, HomeActivity::class.java)
|
||||||
|
startActivity(intent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示"连接网络"按钮
|
||||||
|
*/
|
||||||
|
private fun showNetworkButton() {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁用返回键
|
||||||
|
*/
|
||||||
|
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
||||||
|
return if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||||
|
// 禁用返回键,不执行任何操作
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
super.onKeyDown(keyCode, event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -3,7 +3,7 @@ package com.sw.inbound.receiver
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.sw.inbound.activity.HomeActivity
|
import com.sw.inbound.activity.InitActivity
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,7 +14,7 @@ class BootReceiver : BroadcastReceiver() {
|
|||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
|
if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
|
||||||
Timber.d("设备启动完成,开始执行自启动逻辑")
|
Timber.d("设备启动完成,开始执行自启动逻辑")
|
||||||
val intent = Intent(context, HomeActivity::class.java)
|
val intent = Intent(context, InitActivity::class.java)
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.sw.inbound.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>
|
||||||
|
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@mipmap/bg"
|
||||||
|
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">
|
||||||
|
|
||||||
|
<!-- 加载动画 -->
|
||||||
|
<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>
|
||||||
Reference in New Issue
Block a user