Android examples for Network:Network Operation
wait For Wifi Or Network On
//package com.java2s; import android.content.Context; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.telephony.TelephonyManager; public class Main { public static void waitForWifiOrNetworkOn(Context context) throws Exception { boolean isWifiOrNetworkOn = false; long startTime = System.currentTimeMillis(); do {/*from w ww .j a va2 s. c o m*/ Thread.sleep(500); isWifiOrNetworkOn = isWifiOn(context) || isNetworkOn(context); } while (!isWifiOrNetworkOn && (System.currentTimeMillis() - startTime < 10000)); if (!isWifiOrNetworkOn) { throw new Exception("Wifi and Network is not turned on"); } } /** * Verify is Wifi network on now * * @param context * - Context * @return true if Wifi is switched on * @throws SettingNotFoundException */ public static boolean isWifiOn(Context context) throws SettingNotFoundException { int value = Settings.System.getInt(context.getContentResolver(), Settings.Secure.WIFI_ON); return value == 0 ? false : true; } /** * @param context * - Context * @return true if it is mobile network connection */ public static boolean isNetworkOn(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED ? true : false; } }