2
public class Information extends BroadcastReceiver{
   public static String ChangeDate;

   @Override
   public void onReceive(Context context, Intent intent) {  
      ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

      android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

      if (wifi.isAvailable() || mobile.isAvailable()) {
         Toast.makeText(context, "Internet Available", Toast.LENGTH_SHORT).show();
      } else {
         Toast.makeText(context, "Internet not Available", Toast.LENGTH_SHORT).show();
                         RahuActivity.nextAlaram(); 
      }

   }

}

Manifest.xml

   <receiver android:name="com.astrobix.service.Information" >
    <intent-filter>

            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />


        </intent-filter>
</receiver>

Please anyone help me when I run this code on my mobile it shows both Toast methods. When I turn off Wi-Fi at first it show me Internet Available message and again show me Internet not Available message. It must show only onemessage, I think that's why my RahuActivity.nextAlaram() shows an exception.

Logcat here :-
08-02 16:25:54.309: E/AndroidRuntime(21170): java.lang.RuntimeException: Unable to start receiver com.astrobix.service.Information: java.lang.NullPointerException
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
FarhaSameer786
  • 370
  • 1
  • 4
  • 12

3 Answers3

1

Try This Function

//Function For Check WIFI Connection
    public boolean fun_CheckWIFIConnection(String WIFIName) {

        ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (networkInfo.isConnected()) {

                final WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();

                if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {

                    String SSID = connectionInfo.getSSID();

                    if (SSID.startsWith("\""))
                    {
                        SSID = SSID.substring(1, SSID.length());
                    }
                    if (SSID.endsWith("\""))
                    {
                        SSID = SSID.substring(0, (SSID.length() -1));
                    }
                    if (SSID.equals(WIFIName))
                    {
                        return true;
                    }

                }
                else{
                    Log.e("Request Alert", "connectionInfo Is null OR SSID Is Blanck");
                    return false;
                }
            }
            else{
                    Log.e("Request Alert", "WIFI Is Not Connected");
                    return false;

            }

        return false;
    }
Karan Mavadhiya
  • 1,042
  • 8
  • 23
0

You should try mobile.isConnected() ||wifi.isConnected() rather than isAvailable() Hope it helps

ZeusNet
  • 710
  • 9
  • 25
  • @Holger-i also tried but not working..but when i run this on emulator and press F8 it shows the correct msg according to availability or not – FarhaSameer786 Aug 02 '13 at 11:11
  • Then I think something went wrong with you device. But I'm not sure were the problem could be. Because if you got both messeages. It seems that you connection-adapters are switching from available to not available in a very short time – ZeusNet Aug 02 '13 at 11:13
0

This may help, the obvious one in android!!

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;
}
Exceptional
  • 2,994
  • 1
  • 18
  • 25