Question

I need Help. I want a toast to show the Internet status in my application. It is working good for wifi and mobile data packet (Mobile Internet ). But when Internet is totally disconnected it shows nothing. I don't know how to do this. here is my code

public class ConnectivityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("myapp", "action: " + intent.getAction());
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&
                          activeNetwork.isConnectedOrConnecting();

     if(isConnected)
     {
     if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
     {
         Toast toast = Toast.makeText(context, "Connected to wifi", Toast.LENGTH_LONG);
         toast.show();
     }
     else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
     {
         Toast toast = Toast.makeText(context, "Connected to Mobile Data Pakage", Toast.LENGTH_LONG);
         toast.show();

     }
     else 
      {
         Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
         toast.show();

      }

also try this but compiler show error. Kindly help me. Thanks in advance.

 else if(activeNetwork.getType() == ConnectivityManager.EXTRA_NO_CONNECTIVITY)
      {
                                                                                  Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
         toast.show();

Manifesto

 <receiver android:name="com.networkstatechange.ConnectivityReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
Was it helpful?

Solution 2

With correct indentation you would have seen your toast 'Not connected' is only displayed if you enter the block if (isConnected). Rewrite this way:

 if(isConnected)
 {
   if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
   {
       Toast toast = Toast.makeText(context, "Connected to wifi", Toast.LENGTH_LONG);
       toast.show();
   }
   else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
   {
       Toast toast = Toast.makeText(context, "Connected to Mobile Data Pakage", Toast.LENGTH_LONG);
       toast.show();

   }
 }
 else 
  {
     Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
     toast.show();

  }
 }

OTHER TIPS

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

}

creat this class

and use isConnecting to internet method in your Receivers onreceive it should work fine

haply coding :)

Try this

public static boolean checkConn(Context ctx) {
       ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo i = conMgr.getActiveNetworkInfo();
       if (i == null)
        return false;
        if (!i.isConnected())
        return false;
        if (!i.isAvailable())
        return false;
        return true;        
    }

Also use..

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top