2009-11-23 191 views
60

我想檢查Android手機的網絡何時關閉。我可以捕捉該事件嗎?網絡監聽器Android

我沒有得到適當的API或任何可以解釋相同的示例。如果有人做了或者任何示例鏈接都會非常有幫助。

回答

144

新的Java類:

public class ConnectionChangeReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo( ConnectivityManager.TYPE_MOBILE); 
    if (activeNetInfo != null) 
    { 
     Toast.makeText(context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
    } 
    if(mobNetInfo != null) 
    { 
     Toast.makeText(context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
    } 
    } 
} 

在AndroidManifest.xml中新的XML的 「清單」 元素:

<!-- Needed to check when the network connection changes --> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

在AndroidManifest.xml中新的XML 「應用程序」 元素:

<receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver" 
      android:label="NetworkConnection"> 
    <intent-filter> 
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
    </intent-filter> 
</receiver> 
+0

嘿感謝它按預期工作 – Sam97305421562 2009-11-24 09:56:36

+0

作爲@noillusioin說,activeNetInfo可以爲空。這表示如果您知道(保存狀態)您之前連接了網絡連接,則表示網絡連接已斷開。 – larham1 2013-04-18 21:10:28

+0

您還需要檢查NetworkInfo.isConnected – shuriquen 2015-04-18 21:25:21

7

上述答案僅適用於移動分組數據已啓用。否則,ConnectivityManager將爲空,並且不能再檢索NetworkInfo。解決方法是使用PhoneStateListener或TelephonyManager。

16

我一直在使用一個小設置來檢查帶寬,以確定如何縮放事物,如圖像。

下的活性,在AndroidManifest:

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

在活動其中正在執行的檢查:然後

boolean network; 
int bandwidth; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ... 
    network = isDataConnected(); 
    bandwidth = isHighBandwidth(); 
    registerReceiver(new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      network = isDataConnected(); 
      bandwidth = isHighBandwidth(); 
     } 
    }, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); 
    ... 
} 
... 
private boolean isDataConnected() { 
    try { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 
    } catch (Exception e) { 
     return false; 
    } 
} 

private int isHighBandwidth() { 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo info = cm.getActiveNetworkInfo(); 
    if (info.getType() == ConnectivityManager.TYPE_WIFI) { 
     WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     return wm.getConnectionInfo().getLinkSpeed(); 
    } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { 
     TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     return tm.getNetworkType(); 
    } 
    return 0; 
} 

一個示例用法是:

if (network) { 
    if (bandwidth > 16) { 
     // Code for large items 
    } else if (bandwidth <= 16 && bandwidth > 8) { 
     // Code for medium items 
    } else { 
     //Code for small items 
    } 
} else { 
    //Code for disconnected 
} 

這是不是最漂亮的,但它具有足夠的靈活性,可以改變項目de的帶寬截止值等待他們是什麼和我的要求。

+0

找到這個非常有用,謝謝:) – Skynet 2014-02-06 10:15:20

9

如果使用Android Annotations是你在你的活動試試這個選項 - 這一切,產生其餘的:

@Receiver(actions = ConnectivityManager.CONNECTIVITY_ACTION, 
     registerAt = Receiver.RegisterAt.OnResumeOnPause) 
void onConnectivityChange() { 
    //react 
} 

使用這個只有當你已經在使用AndroidAnnotations - 把你的項目中這種依賴性只爲這段代碼會過度殺傷。

+0

你可以請把更多的解釋關於這個答案。我試圖找到一些東西,但我不能。 Google註釋庫中沒有@Receiver註釋。謝謝! – Sniper 2016-02-10 07:39:15

+0

嘿,我指的是第三方庫AndroidAnnotations。我更新我的答案。 – 2016-02-11 18:49:22

+0

是的你是對的,有了這個庫,它是可能的,我也發現這個https://github.com/jd-alexander/flender檢查它不壞,但你只能用它與gradle build 1。3,新版本的gradle插件不起作用 – Sniper 2016-02-11 21:36:58