如何检查网络和WI-FI可用性

时间:2013-08-02 11:03:39

标签: android broadcastreceiver

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>

当我在我的手机上运行此代码时,请任何人帮助我,它会显示两种Toast方法。 当我首先关闭Wi-Fi时,它会向我显示Internet Available消息并再次显示给我 互联网不可用消息。它必须只显示一个消息,我认为这就是原因 我的RahuActivity.nextAlaram()显示例外。

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

4 个答案:

答案 0 :(得分:2)

试试这个

public class AppStatus {
private static AppStatus instance = new AppStatus();
static Context context;
ConnectivityManager connectivityManager;
NetworkInfo wifiInfo, mobileInfo;
boolean connected = false;

public static AppStatus getInstance(Context ctx) {
    context = ctx;
    return instance;
}

public boolean isOnline(Context con) {
    try {
        connectivityManager = (ConnectivityManager) con
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    connected = networkInfo != null && networkInfo.isAvailable() &&
            networkInfo.isConnected();
    return connected;


    } catch (Exception e) {
        System.out.println("CheckConnectivity Exception: " + e.getMessage());
        Log.e("connectivity", e.toString());
    }
    return connected;
  }
}

并在你的代码中写。

if(AppStatus.isOnline(this)){

  Log.i("Internet Available...");

}else{

  Log.i("No Internet Available...");

}

答案 1 :(得分:1)

尝试此功能

//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;
    }

答案 2 :(得分:0)

您应该尝试mobile.isConnected() ||wifi.isConnected()而不是isAvailable() 希望它有所帮助

答案 3 :(得分:0)

这可能会有所帮助,这在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;
}