2012-08-22 89 views
0

到目前爲止,我試圖通過使用RadioInfo類來驗證哪個網絡已打開並檢查RadioInfo.getNumberOfPacketsReceived()\Sent()中是否有任何更改,但是如果使用此方法多於一個網絡開啓。任何人都可以指向正確的方向。如何確定當設備連接到互聯網時使用哪個網絡

對不起,拼寫和語法,英語不是我的第一語言。

+0

你可以給一個標誌你想達到什麼?即使wifi已啓用某些應用程序仍然可以使用無線連接 –

+0

我想調整一個Android應用程序,通過移動網絡和WiFi測量互聯網流量。在Android中,我使用\ TrafficStats.getMobileRxBytes \和\ TrafficStats.getTotalRxBytes \。在blackberry SDK中,所有統計信息都封裝在\ RadioInfo.getNumberOfPacketsReceived()\ Sent()\中,所以現在我需要確定在其他應用使用互聯網時使用哪個網絡。 – Titus

回答

0

您需要在URL後附加連接類型。

public static String getConnectionString() { 

String connectionString = null; 

// Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR 
// variable. 
if (DeviceInfo.isSimulator()) { 

    connectionString = ";deviceside=true"; 
} 

// Wifi is the preferred transmission method 
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { 

    connectionString = ";interface=wifi"; 
} 

// Is the carrier network the only way to connect? 
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { 

    String carrierUid = getCarrierBIBSUid(); 

    if (carrierUid == null) { 
     // Has carrier coverage, but not BIBS. So use the carrier's TCP 
     // network 

     connectionString = ";deviceside=true"; 
    } else { 
     // otherwise, use the Uid to construct a valid carrier BIBS 
     // request 

     connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public"; 
    } 
} 

// Check for an MDS connection instead (BlackBerry Enterprise Server) 
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { 

    connectionString = ";deviceside=false"; 
} 

// If there is no connection available abort to avoid hassling the user 
// unnecssarily. 
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) { 
    connectionString = "none"; 

} 

// In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ... 
else { 

    connectionString = ";deviceside=true"; 
} 



return connectionString; 

} 

/** 
* Looks through the phone's service book for a carrier provided BIBS 
* network 
* 
* @return The uid used to connect to that network. 
*/ 
private synchronized static String getCarrierBIBSUid() { 
ServiceRecord[] records = ServiceBook.getSB().getRecords(); 
int currentRecord; 

for (currentRecord = 0; currentRecord < records.length; currentRecord++) { 
    if (records[currentRecord].getCid().toLowerCase().equals("ippp")) { 
     if (records[currentRecord].getName().toLowerCase() 
       .indexOf("bibs") >= 0) { 
      return records[currentRecord].getUid(); 
     } 
    } 
} 

return null; 
} 

一旦你寫這段代碼。使用

con = (HttpConnection) Connector.open(url + getConnectionString()); 

它將確定可用的網絡類型。

+0

感謝您的迴應和鱈魚樣本,但我不想通過開放式網絡建立與互聯網的連接。我想確定當其他應用程序使用互聯網時使用哪個網絡。 – Titus

相關問題