2013-08-02 53 views
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如何檢查網絡和Wi-Fi可用性

<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> 

請人幫助我,當我跑我的手機驗證碼它顯示兩個麪包的方法。 當我首先關閉Wi-Fi時,會顯示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 
+0

@Jashan PJ-我無法理解你的話,先生.. – FarhaSameer786

+0

@ JashanPJ亞先生感謝... :-) – FarhaSameer786

回答

0

你應該嘗試mobile.isConnected() ||wifi.isConnected()而不是isAvailable() 希望它可以幫助

+0

@霍爾格,我也試過,但沒有working..but當我在模擬器上運行此並按F8它顯示正確的味精根據可用性或不 – FarhaSameer786

+0

然後我覺得你的設備出了問題。但我不確定問題可能出在哪裏。因爲如果你有兩個消息。看起來你連接適配器正在從可用到不可用在很短的時間 – ZeusNet

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

嘗試這個

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

@Farha我認爲這是一個工作解決方案。 –

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