2011-11-14 24 views
3

我爲android.net.conn.CONNECTIVITY_CHANGE創建了一個過濾器。
我確實收到我的廣播接收者的意圖。connectivitymanager - android.net.conn.CONNECTIVITY_CHANGE

我的問題是關於什麼引發了connectivity_change。 API描述說:

網絡連接發生了變化。連接有 已建立或丟失。受影響網絡的NetworkInfo爲 作爲額外發送;應該查看它是否發生了什麼樣的連接事件。

看來我的broadcastreceiver只有在移動網絡連接/斷開連接時纔會調用(pdp已損壞)。當例如2g切換到3g時不會引發。

我能不能用這個廣播接收器接收2g到3G的互換?
我是否必須使用phonestatelistener來替換例如2g到3g的交換?

+0

檢查這個話題。 http://stackoverflow.com/questions/1783117/network-listener-android –

+0

在該線程的代碼看起來像我做了什麼,但似乎只趕上從移動網絡斷開連接(和多數民衆也什麼看起來像該線程中的人正在尋找),而不是從2g切換到3g – ziggestardust

回答

4

您需要:

permission "android.permission.READ_PHONE_STATE" //獲取連接的變化(2G/3G /等) permission "android.permission.ACCESS_COARSE_LOCATION" //得到小區/大廈變化

//Make the listener 
listener = new PhoneStateListener() { 
    public void onDataConnectionStateChanged(int state, int networkType) 
    { 
     //We have changed proticols, for example we have gone from HSDPA to GPRS 
     //HSDPA is an example of a 3G connection 
     //GPRS is an example of a 2G connection 
    } 
    public void onCellLocationChanged(CellLocation location) { 
     //We have changed to a different Tower/Cell 
    } 
}; 

//Add the listener made above into the telephonyManager 
telephonyManager.listen(listener, 
     PhoneStateListener.LISTEN_DATA_CONNECTION_STATE //connection changes 2G/3G/etc 
     | PhoneStateListener.LISTEN_CELL_LOCATION  //or tower/cell changes 
); 
+0

我相信你是正確的。似乎還沒有辦法通過廣播接收器獲得它。 – ziggestardust

相關問題