ソースを参照

操作完成后自动关闭app

xiongweixp 2 ヶ月 前
コミット
20478639e2

+ 1 - 1
.idea/misc.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ExternalStorageConfigurationManager" enabled="true" />
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="21" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">

+ 37 - 0
app/src/main/java/tech/xiongweixp/touchaction/MainActivity.kt

@@ -18,6 +18,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
 import tech.xiongweixp.touchaction.ui.theme.TouchActionTheme
 import java.net.HttpURLConnection
 import java.net.URL
+import java.util.*
 
 class MainActivity : ComponentActivity() {
     private var computerLayout: ConstraintLayout? = null
@@ -27,6 +28,8 @@ class MainActivity : ComponentActivity() {
     private var compterCloseBtn: Button? = null
     private var lightTextView: TextView? = null
     private var lightCloseBtn: Button? = null
+    private var countDownText1: TextView? = null
+    private var countDownText2: TextView? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -39,6 +42,9 @@ class MainActivity : ComponentActivity() {
         lightTextView = findViewById(R.id.lightText)
         lightCloseBtn = findViewById(R.id.lightCloseButton)
 
+        countDownText1 = findViewById(R.id.coundownText1)
+        countDownText2 = findViewById(R.id.coundownText2)
+
         compterCloseBtn!!.setOnClickListener {
             //退出APP
             finish()
@@ -105,9 +111,11 @@ class MainActivity : ComponentActivity() {
             when(device) {
                 0 -> {
                     computerTextView!!.text = msg
+                    countdownAndQuit(device)
                 }
                 1 -> {
                     lightTextView!!.text = msg
+                    countdownAndQuit(device)
                 }
                 else -> {
                     mainLayout!!.visibility = ConstraintLayout.VISIBLE
@@ -115,6 +123,35 @@ class MainActivity : ComponentActivity() {
             }
         }
     }
+
+    private fun countdownAndQuit(device: Int){
+        var count = 3
+        var timer = Timer()
+        var timerTask = object : TimerTask() {
+            override fun run() {
+                runOnUiThread {
+                    if (count > 0) {
+                        when(device) {
+                            0 -> {
+                                countDownText1!!.text = "${count}秒后退出"
+                            }
+                            1 -> {
+                                countDownText2!!.text = "${count}秒后退出"
+                            }
+                        }
+                        // computerTextView!!.text = "操作成功,${count}秒后退出"
+                        count--
+                    } else {
+                        // computerTextView!!.text = "操作成功,退出"
+                        timer.cancel()
+                        finish()
+                    }
+                }
+            }
+        }
+        timer.schedule(timerTask, 0, 1000)
+    }
+
     private fun dealAction() {
         Log.i("MainActivity", "感应到标签")
         var uri = intent.data

+ 16 - 2
app/src/main/res/layout/mainlayout.xml

@@ -80,12 +80,19 @@
                 app:layout_constraintTop_toBottomOf="@+id/imageView" app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                 android:layout_marginBottom="295dp"/>
+        <TextView
+                android:text=" "
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content" android:id="@+id/coundownText1"
+                android:layout_marginTop="10dp"
+                app:layout_constraintTop_toBottomOf="@+id/computerText" app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintEnd_toEndOf="parent"/>
         <Button
                 android:text="关闭"
                 android:layout_width="316dp"
                 android:layout_height="57dp" android:id="@+id/computerCloseButton"
                 android:layout_marginTop="40dp"
-                app:layout_constraintTop_toBottomOf="@+id/computerText" app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/coundownText1" app:layout_constraintStart_toStartOf="parent"
                 android:layout_marginStart="51dp" android:layout_marginEnd="44dp"
                 app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                 android:layout_marginBottom="198dp" android:background="@drawable/button_shape"
@@ -114,12 +121,19 @@
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                 android:layout_marginBottom="295dp"/>
+        <TextView
+                android:text=" "
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content" android:id="@+id/coundownText2"
+                android:layout_marginTop="10dp"
+                app:layout_constraintTop_toBottomOf="@+id/lightText" app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintEnd_toEndOf="parent"/>
         <Button
                 android:text="关闭"
                 android:layout_width="316dp"
                 android:layout_height="57dp" android:id="@+id/lightCloseButton"
                 android:layout_marginTop="40dp"
-                app:layout_constraintTop_toBottomOf="@+id/lightText" app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toBottomOf="@+id/coundownText2" app:layout_constraintStart_toStartOf="parent"
                 android:layout_marginStart="51dp" android:layout_marginEnd="44dp"
                 app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                 android:layout_marginBottom="198dp" android:background="@drawable/button_shape"