2023-03-19

【Kotlin】ping 判斷 android 裝置是否連線至網際網路

參考資料 ----







以下備存,做為學習歷程的記錄


在爬文的過程中, 也有看到認為這不可靠的觀點......所以,再繼續尋找更好的做法吧......

===== 2023.03.23 =====
拿手邊的幾支 Android 裝置實測,在同樣的 Wi-Fi 環境中
ASUS Zenfone3(Android8.0) -- 正常
華為 T3 平板(Android7.0) -- 反應網路不通,但 app 內的 AdMob 廣告播放正常,表示 app 在 T3 執行時誤判。
SugarY12S(AndroidGo8.1.0) -- 正常
ASUS Zenfone5(Android9) -- 正常
小米 POCO X4 PRO(Android12) -- 正常


app 層級的 build.gradle

  1.  
  2. ...
  3. ...
  4.  
  5. android {
  6. ...
  7. ...
  8. buildFeatures {
  9. viewBinding true
  10. }
  11. buildTypes {
  12. ...
  13. ...
  14. }
  15. }
  16.  
  17. dependencies {
  18. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
  19. }
  20. ...
  21. ...
  22.  



AndroidManifest.xml
  1.  
  2.  
  3. <uses-permission android:name="android.permission.INTERNET"/>
  4.  
  5. <application
  6. ...
  7. ...
  8.  


activity_main.xml
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <androidx.constraintlayout.widget.ConstraintLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. tools:context=".MainActivity">
  10.  
  11. <TextView
  12. android:id="@+id/lblHello"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:onClick="isConnected"
  16. android:text="Hello World!"
  17. android:textSize="20sp"
  18. app:layout_constraintBottom_toBottomOf="parent"
  19. app:layout_constraintEnd_toEndOf="parent"
  20. app:layout_constraintStart_toStartOf="parent"
  21. app:layout_constraintTop_toTopOf="parent" />
  22.  
  23. </androidx.constraintlayout.widget.ConstraintLayout>
  24.  



MainActivity.kt
  1.  
  2. import android.content.ContentValues.TAG
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.util.Log
  6. import android.view.View
  7. import android.widget.Toast
  8. import com.example.pingconn.databinding.ActivityMainBinding
  9. import kotlinx.coroutines.Dispatchers
  10. import kotlinx.coroutines.GlobalScope
  11. import kotlinx.coroutines.launch
  12. import kotlinx.coroutines.withContext
  13. import java.io.IOException
  14. import java.net.InetAddress
  15.  
  16. class MainActivity : AppCompatActivity() {
  17.  
  18. val TAG = "MainActivity"
  19. private lateinit var binding: ActivityMainBinding
  20.  
  21. override fun onCreate(savedInstanceState: Bundle?) {
  22. super.onCreate(savedInstanceState)
  23. binding = ActivityMainBinding.inflate(layoutInflater)
  24. val view = binding.root
  25. setContentView(view)
  26. }
  27.  
  28. fun isConnected(vv: View)
  29. {
  30. // Toast.makeText(this, "test", Toast.LENGTH_SHORT).show()
  31. GlobalScope.launch {
  32. val address = "8.8.8.8"
  33. val isReachable = myPing(address)
  34. withContext(Dispatchers.Main) {
  35. updateUI(isReachable)
  36. }
  37. }
  38. }
  39.  
  40. private fun updateUI(isReachable: Boolean) {
  41. if(isReachable)
  42. binding.lblHello.text = "internet connected"
  43. else
  44. binding.lblHello.text = "no internet"
  45. }
  46.  
  47. suspend fun myPing(address: String): Boolean = withContext(Dispatchers.IO) {
  48. try {
  49. val inetAddress = InetAddress.getByName(address)
  50. inetAddress.isReachable(500) // 單位:毫秒,
  51. } catch (e: Exception) {
  52. false
  53. }
  54. }
  55. }
  56.  


相關筆記 ----

沒有留言:

張貼留言