ubuntu 在 R40e 上 還有 Debian 在 Sempron 2600 上

2012年5月10日 星期四

android : reconnect 3G after disconect wifi

這個 2.3.3 版 android,在 wifi 斷線的時候,好像不會自動把 3G 連回去喔..

之後手動去 mobile data 關掉再開一次,3G 才會繼續連線。

... 但是手機連 2.2. 的都會呀!!!

連線"狀態" 的處理,好像是 ConnectivityService.java 負責的。
每個DISCONNECT, CONNECT 都會有 event 過來。

他負責各 network 的相互關係,例如以上的動作,就是由他完成的。
wifi 只要負責開,關就可以。
wifi 開了之後,ConnectivityService 會知道,他就幫你把其他的網路都關掉。

所以改這裡應該可以叫他把 3g 接回去..


懶得寫,直接貼 diff: services/java/com/android/server/ConnectivityService.java patch | blob | history diff --git a/services/java/com/android/server/ConnectivityService.java b/services/java/com/android/server/ConnectivityService.java index c782e4e..76c5a15 100644 (file) --- a/services/java/com/android/server/ConnectivityService.java +++ b/services/java/com/android/server/ConnectivityService.java @@ -58,7 +58,7 @@ import java.util.List; */ public class ConnectivityService extends IConnectivityManager.Stub { - private static final boolean DBG = false; + private static final boolean DBG = true; private static final String TAG = "ConnectivityService"; // how long to wait before switching back to a radio's default network @@ -161,6 +161,9 @@ public class ConnectivityService extends IConnectivityManager.Stub { private static final int EVENT_SET_MOBILE_DATA = MAX_NETWORK_STATE_TRACKER_EVENT + 7; + private static final int EVENT_ENABLE_MOBILE_DATA_AGAIN = + MAX_NETWORK_STATE_TRACKER_EVENT + 8; + private Handler mHandler; // list of DeathRecipients used to make sure features are turned off when @@ -896,6 +899,16 @@ public class ConnectivityService extends IConnectivityManager.Stub { (enabled ? ENABLED : DISABLED), 0)); } + /** + * don't care about current state, enable mobiledata anyway + */ + private void enableMobileDataAgain() { + enforceChangePermission(); + if (DBG) Slog.d(TAG,"enableMobileDataAgain"); + + mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_MOBILE_DATA_AGAIN,0,0)); + } + private void handleSetMobileData(boolean enabled) { if (getMobileDataEnabled() == enabled) return; @@ -921,6 +934,15 @@ public class ConnectivityService extends IConnectivityManager.Stub { } } + private void handleEnableMobileDataAgain() { + if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) { + if (DBG) { + Slog.d(TAG, "enable again : " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]); + } + mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect(); + } + } + private int getNumConnectedNetworks() { int numConnectedNets = 0; @@ -969,6 +991,12 @@ public class ConnectivityService extends IConnectivityManager.Stub { int prevNetType = info.getType(); + if(prevNetType == ConnectivityManager.TYPE_WIFI) + if(getMobileDataEnabled()){ + Slog.d(TAG,"wifi disc, recover mobile data"); + enableMobileDataAgain(); + } + mNetTrackers[prevNetType].setTeardownRequested(false); /* * If the disconnected network is not the active one, then don't report @@ -1607,6 +1635,11 @@ public class ConnectivityService extends IConnectivityManager.Stub { handleSetMobileData(enabled); break; } + case EVENT_ENABLE_MOBILE_DATA_AGAIN: + { + handleEnableMobileDataAgain(); + break; + } } } }

其實沒有處理 connectionfail 的情況...

沒有留言:

標籤

網誌存檔