[英]Manually starting 3G connection in Android, and keeping it on
在啟用WiFi的同時,如何在Android中啟動3G數據連接? 我試過了
IConnectivityManager.setMobileDataEnabled(enabled); // via reflection
它可以在模擬器中運行,但在我的真實手機(Droid 2)中,它會短暫打開然后再次關閉。
從shell(adb shell), ip link
提供3G連接的詳細信息:
15: ppp0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 3 link/ppp
但是,它僅在WiFi關閉時可用。 當WiFi打開並且我嘗試手動打開它時,它會抱怨ppp0設備不存在。
bash-3.2# ip link set ppp0 up
ip link set ppp0 up
Cannot find device "ppp0"
當我嘗試列出設備時,我甚至找不到它
bash-3.2# ls /dev/ppp*
ls /dev/ppp*
/dev/ppp
據我所知,在不修改Android平台源代碼(至少版本2.3和4)的情況下,不可能同時連接3g和WiFi。 主要問題是frameworks / base / core / res / res / values / config.xml中定義的連接的硬編碼優先級:
<!-- This string array should be overridden by the device to present a list of network
attributes. This is used by the connectivity manager to decide which networks can coexist
based on the hardware -->
<!-- An Array of "[Connection name],[ConnectivityManager connection type],
[associated radio-type],[priority] -->
<!-- ^^^^^^^^^^---------- Connection priority -->
<string-array translatable="false" name="networkAttributes">
<item>"wifi,1,1,1"</item>
<item>"mobile,0,0,0"</item>
<item>"mobile_mms,2,0,2"</item>
<item>"mobile_supl,3,0,2"</item>
<item>"mobile_hipri,5,0,3"</item>
</string-array>
然后,此connect.xml由ConnectivityService讀取,該服務訂閱了connect / disconnect事件。 在connect handler中,它決定了它應該對其他連接做什么:
private void handleConnect(NetworkInfo info) {
//------------8-<--------------------------
// if this is a default net and other default is running
// kill the one not preferred
if (mNetAttributes[type].isDefault()) {
if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) {
if ((type != mNetworkPreference &&
mNetAttributes[mActiveDefaultNetwork].mPriority >
// ^^^^^^^^^ --- From config.xml
mNetAttributes[type].mPriority) ||
// ^^^^^^^^^-------- From config.xml
mNetworkPreference == mActiveDefaultNetwork) {
// don't accept this one
if (DBG) Slog.v(TAG, "Not broadcasting CONNECT_ACTION " +
"to torn down network " + info.getTypeName());
teardown(thisNet);
return;
//------------8-<--------------------------
您可以嘗試通過修改連接服務來同時保持活動狀態,但我建議不要這樣做,因為它很可能會破壞您的電池續航時間。
如果你想嘗試一下,請看這里 (並確保你有一個備份,顯然)
如果您嘗試連接到特定計算機,可以嘗試使用ConnectivityManager.requestRouteToHost 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.