Search in sources :

Example 11 with ConnectivityManager

use of android.net.ConnectivityManager in project android-app by eoecn.

the class NetWorkHelper method isMobileDataEnable.

/**
	 * 判断MOBILE网络是否可用
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
public static boolean isMobileDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isMobileDataEnable = false;
    isMobileDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    return isMobileDataEnable;
}
Also used : ConnectivityManager(android.net.ConnectivityManager)

Example 12 with ConnectivityManager

use of android.net.ConnectivityManager in project android-job by evernote.

the class Device method getNetworkType.

@NonNull
public static JobRequest.NetworkType getNetworkType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
        return JobRequest.NetworkType.ANY;
    }
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null && telephonyManager.isNetworkRoaming()) {
        return JobRequest.NetworkType.CONNECTED;
    }
    boolean metered = ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager);
    return metered ? JobRequest.NetworkType.NOT_ROAMING : JobRequest.NetworkType.UNMETERED;
}
Also used : NetworkInfo(android.net.NetworkInfo) TelephonyManager(android.telephony.TelephonyManager) ConnectivityManager(android.net.ConnectivityManager) NonNull(android.support.annotation.NonNull)

Example 13 with ConnectivityManager

use of android.net.ConnectivityManager in project android_frameworks_base by ParanoidAndroid.

the class MobileDataButton method toggleState.

@Override
protected void toggleState(Context context) {
    boolean enabled = getDataState(context);
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (enabled) {
        cm.setMobileDataEnabled(false);
    } else {
        cm.setMobileDataEnabled(true);
    }
}
Also used : ConnectivityManager(android.net.ConnectivityManager)

Example 14 with ConnectivityManager

use of android.net.ConnectivityManager in project android_frameworks_base by ParanoidAndroid.

the class WifiWatchdogStateMachine method makeWifiWatchdogStateMachine.

public static WifiWatchdogStateMachine makeWifiWatchdogStateMachine(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    sWifiOnly = (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false);
    // Watchdog is always enabled. Poor network detection can be seperately turned on/off
    // TODO: Remove this setting & clean up state machine since we always have
    // watchdog in an enabled state
    putSettingsGlobalBoolean(contentResolver, Settings.Global.WIFI_WATCHDOG_ON, true);
    WifiWatchdogStateMachine wwsm = new WifiWatchdogStateMachine(context);
    wwsm.start();
    return wwsm;
}
Also used : ConnectivityManager(android.net.ConnectivityManager) ContentResolver(android.content.ContentResolver)

Example 15 with ConnectivityManager

use of android.net.ConnectivityManager in project robotium by RobotiumTech.

the class SystemUtils method setMobileData.

/**
	 * Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
	 * 
	 * @param turnedOn true if mobile data is to be turned on and false if not
	 */
public void setMobileData(Boolean turnedOn) {
    ConnectivityManager dataManager = (ConnectivityManager) instrumentation.getTargetContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    Method dataClass = null;
    try {
        dataClass = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
        dataClass.setAccessible(true);
        dataClass.invoke(dataManager, turnedOn);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Method(java.lang.reflect.Method)

Aggregations

ConnectivityManager (android.net.ConnectivityManager)361 NetworkInfo (android.net.NetworkInfo)258 Context (android.content.Context)15 TelephonyManager (android.telephony.TelephonyManager)13 RemoteException (android.os.RemoteException)11 Test (org.junit.Test)11 Intent (android.content.Intent)10 WifiManager (android.net.wifi.WifiManager)9 Method (java.lang.reflect.Method)9 Network (android.net.Network)8 IOException (java.io.IOException)8 PackageManager (android.content.pm.PackageManager)7 SharedPreferences (android.content.SharedPreferences)6 NetworkCapabilities (android.net.NetworkCapabilities)6 UserManager (android.os.UserManager)6 Activity (android.app.Activity)5 ApplicationInfo (android.content.pm.ApplicationInfo)5 IPackageManager (android.content.pm.IPackageManager)5 PackageInfo (android.content.pm.PackageInfo)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5