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(); 
      }

   }

}

清单.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 可用消息,然后再次显示 Internet 不可用消息。它必须只显示一个消息,我认为这就是为什么我的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

4 回答 4

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...");

}
于 2013-08-02T11:17:52.947 回答
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;
    }
于 2013-08-02T11:09:41.337 回答
0

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

于 2013-08-02T11:08:19.370 回答
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;
}
于 2013-08-02T12:06:29.497 回答