Android ez 2013-06-21
利用 NetworkInfo 檢查 Android 的網路狀態,並且吐出 JSON 格式。
public String NetworkInfo() { ConnectivityManager CM = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = CM.getActiveNetworkInfo(); if(info!=null) return "{ "+ "getTypeName:\""+info.getTypeName()+"\","+ "getState:\""+info.getState()+"\","+ "isAvailable:\""+info.isAvailable()+"\","+ "isConnected:\""+info.isConnected()+"\","+ "isConnectedOrConnecting:\""+info.isConnectedOrConnecting()+"\","+ "isFailover:\""+info.isFailover()+"\","+ "isRoaming:\""+info.isRoaming()+"\""+ "}"; else return "{}"; }
說明:
info.getTypeName(); // 目前以何種方式連線 [WIFI] info.getState(); // 目前連線狀態 [CONNECTED] info.isAvailable(); // 目前網路是否可使用 [true] info.isConnected(); // 網路是否已連接 [true] info.isConnectedOrConnecting(); // 網路是否已連接 或 連線中 [true] info.isFailover(); // 網路目前是否有問題 [false] info.isRoaming(); // 網路目前是否在漫遊中 [false]
標籤: Android
本文章網址:
https://www.ez2o.com/Blog/Post/Android-Check-Network-State
https://www.ez2o.com/Blog/Post/387
https://www.ez2o.com/Blog/Post/Android-Check-Network-State
https://www.ez2o.com/Blog/Post/387