Skip to content Skip to sidebar Skip to footer

How To Find Network Enable Status In Android?

Is there possible to find internet enable using intent in broadcast receiver in android pls help me? I want exactly background process when app is not open i want to detect network

Solution 1:

try this below code,

publicclassConnectionAvailableextendsBroadcastReceiver {

@OverridepublicvoidonReceive(Context context, Intent intent) {

    ConnectivityManagermanager= (ConnectivityManager) context
            .getSystemService(context.CONNECTIVITY_SERVICE);
    booleanis3g= manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
    booleanisWifi= manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();

    if (!is3g && !isWifi) {

        Toast.makeText(context, "Internet Connection Lost",
                Toast.LENGTH_LONG).show();

    } else {
        if ((intent.getAction() != null)
                && (intent.getAction()
                        .equals("android.intent.action.AIRPLANE_MODE"))) {
            Toast.makeText(context, "Internet Connection Lost",
                    Toast.LENGTH_LONG).show();               
        }
    }
}
}

This might help you out..

Solution 2:

try the following code:

privatevoidsetMobileDataEnabled(Context context, boolean enabled) {
finalConnectivityManagerconman= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
finalClassconmanClass= Class.forName(conman.getClass().getName());
finalFieldiConnectivityManagerField= conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
finalObjectiConnectivityManager= iConnectivityManagerField.get(conman);
finalClassiConnectivityManagerClass= Class.forName(iConnectivityManager.getClass().getName());
finalMethodsetMobileDataEnabledMethod= iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);

setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
 }

Solution 3:

ConnectivityManagercm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;

Solution 4:

publicstaticbooleanisNetworkAvailable(Context context) {
             ConnectivityManagerconnectivity= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
             if (connectivity != null) {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                   for (inti=0; i < info.length; i++) {
                      if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                          returntrue;
                      }
                   }
                }
             }
             returnfalse;
          }

Solution 5:

publicbooleanisOnline() {
    ConnectivityManagercm=
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfonetInfo= cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        returntrue;
    }
    returnfalse;}
   and add this permission     

call this method and add this permission in your manifest file <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Post a Comment for "How To Find Network Enable Status In Android?"