2014-01-20 89 views
14

我正在開發一個android應用程序,我想在互聯網(wifi或分組數據連接)連接丟失時收到通知。在我的方法,我可以得到的連接爲一體的狀態:android如何在互聯網連接丟失時得到指示?

private boolean isNetworkAvailable() { 
ConnectivityManager connectivityManager 
     = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

而在清單有這樣的:當連接丟失

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

我怎麼會自動通知?

+0

上面的代碼是檢查是否有互聯網的標準方式,所以你的問題是什麼? –

+0

see here http://stackoverflow.com/questions/3119607/how-to-be-notified-on-wifi-network-status-change –

+0

@ user2310289:它需要調用'isNetworkAvailable();'方法來獲取瞭解連接狀態。它確定何時啓動應用程序。但是當應用程序運行它沒有太多的好方法來處理要求 –

回答

8

對於WIFI,你可以註冊一個廣播接收器:

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); 
registerReceiver(broadcastReceiver, intentFilter); 

您還可以註冊在清單中的接收器。

@Override 
public void onReceive(Context context, Intent intent) { 
    final String action = intent.getAction(); 
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { 
     if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){ 
      //do stuff 
     } else { 
      // wifi connection was lost 
     } 
    } 
} 

對於註冊爲任何類型的,你可以使用下面的接收器的數據連接監聽器:

在接收機

然後

<receiver android:name=".receiver.ConnectivityReceiver"> 
    <intent-filter> 
     <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
    </intent-filter> 
</receiver> 

和你ConnectivityReceiver

public class ConnectivityReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    } 
} 

onReceive方法y你可以檢查你是否有互聯網連接或不使用this developer article

+0

可以請你解釋'registerReceiver(broadcastReceiver,intentFilter);' –

+0

如果你有興趣知道'registerReceiver'的生命週期, Activity' /'Service'。否則,你直接在清單中註冊一個'BroadcastReceiver',所以無論應用程序是否在運行,你都可以隨時獲得更新。 ['registerReceiver'](http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,android.content.IntentFilter))可在'Context'類中找到。如果您在'Context'類中註冊,請不要忘記調用unregisterReceiver。 – gunar

+0

非常感謝... –

-2

您應該根據您的要求使用BroadcastReceivers。創建一個偵聽網絡變化的BroadcastReceiver。

如果對網絡進行任何更改,它將自動觸發該事件,並且您可以執行相關操作。

0

您需要設置一個廣播接收器。

將這些添加爲意圖過濾器。

  <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
       <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> 
      </intent-filter> 
3

使用下面的代碼:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import classes.NetworkUtil; 

public class NetworkChangeReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(final Context context, final Intent intent) { 

      boolean IsConnected = NetworkUtil.getConnectivityStatusString(context); 
      // Toast in here, you can retrieve other value like String from NetworkUtil 
      // but you need some change in NetworkUtil Class 
     } 
    } 

NetworkUtil是:

package classes; 

import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 

public class NetworkUtil { 

    public static int TYPE_WIFI = 1; 
    public static int TYPE_MOBILE = 2; 
    public static int TYPE_NOT_CONNECTED = 0; 


    public static int getConnectivityStatus(Context context) { 
     ConnectivityManager cm = (ConnectivityManager) context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
     if (null != activeNetwork) { 
      if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) 
       return TYPE_WIFI; 

      if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) 
       return TYPE_MOBILE; 
     } 
     return TYPE_NOT_CONNECTED; 
    } 

    public static boolean getConnectivityStatusString(Context context) { 
     int conn = NetworkUtil.getConnectivityStatus(context); 
     boolean status = false ; 
     if (conn == NetworkUtil.TYPE_WIFI) { 
      status = true; 
     } else if (conn == NetworkUtil.TYPE_MOBILE) { 
      status = true; 
     } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { 
      status = false; 
     } 
     return status; 
    } 
} 

,並在清單文件:

<receiver 
      android:name="receiver.NetworkChangeReceiver" > 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      </intent-filter> 
     </receiver> 

與此權限:

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

這對我來說非常合適!謝謝 –

相關問題