2023.06.01
ping 的回應速度極快,還可指定等待 timeout 的時間限制,目前為止找到的最佳解
ping 的回應速度極快,還可指定等待 timeout 的時間限制,目前為止找到的最佳解
app 層級 build.gradle
android { ... ... buildFeatures { viewBinding true } } dependencies { ... ... implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4") }
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" /> <application ... ...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/lblHello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:onClick="pingDuration" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
... ... import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch import java.net.InetSocketAddress import java.net.Socket import java.text.SimpleDateFormat import java.util.Date import java.util.Locale class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) pingDuration() } // 偵測 internet 連線狀況 fun pingDuration() { val mScope = CoroutineScope(Job() + Dispatchers.IO) mScope.launch(Dispatchers.IO) { var sDuration = "" try { val a1 = Date() val timeoutMs = 1000 // 設定 timeout 為 1 秒 val socket = Socket() val socketAddress = InetSocketAddress("8.8.8.8", 53) socket.connect(socketAddress, timeoutMs) socket.close() val a2 = Date() val durationMillis = a2.time - a1.time // 平均小於 0.07 秒 val hours = durationMillis / (1000 * 60 * 60) val minutes = durationMillis % (1000 * 60 * 60) / (1000 * 60) val seconds = durationMillis % (1000 * 60) / 1000 val millis = durationMillis % 1000 sDuration = String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, millis) } catch (e: Exception) { sDuration = "斷網 -- "+e.message } runOnUiThread { binding.lblHello.text = sDuration } } } }
沒有留言:
張貼留言