public static boolean isOnline (Context context) {
try {
ConnectivityManager conMan = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected()) {
Toast.makeText(context, "Wifi 已連線", Toast.LENGTH_SHORT);
return true;
} else if (mobileInfo.isConnected()) {
Toast.makeText(context, "3G 已連線", Toast.LENGTH_SHORT);
return true;
} else {
Toast.makeText(context, "未開啟 3G/Wifi 網路,請連線登入!", Toast.LENGTH_SHORT);
return false;
}
} catch(Exception e){
e.printStackTrace();
}
return false;
}
[Android] Check Network Status
訂閱:
張貼留言
(
Atom
)
沒有留言 :
張貼留言