天天看點

Android浏覽器Browser二次開發(四)浏覽器中的APN切換

Android浏覽器Browser二次開發(四)浏覽器中的APN切換

文章分類:移動開發 第四章 浏覽器中的APN切換

業務需求:有些連結需要使用CMWAP接入點才能成功通路, 我們的浏覽器必須能夠自動切換CNNET, CMWAP.

分析:調用浏覽器的時候在Intent中傳遞一個參數, 用于标示接入點需要。 我們的浏覽器需要處理3種情況, 第一是參數為cmwap時, 切換成cmwap; 第二是參數為cmnet或者從桌面上的浏覽器圖示點選, 第一次啟動浏覽器時, 切換成cmnet; 第三是打開一個連結, 沒有參數時, 不做切換, 直接使用目前接入點。 如果已經打開浏覽器, 并且切換成cmwap, 無論後來如何去手動設定接入點, 當點選浏覽器裡面的連結, 或者輸入其他位址進行通路時, 必須能夠再次切換成wap. 已經打開為cmnet浏覽器同理。 在需要切換的時候, 必須等到切換完成, 新的接入點成功連接配接上以後才能開始資料的載入。

改動: 1. 接入點切換方法

public int forceAPNConnection(String apnName) {

//validate apn name.

if (apnName == null || apnName.equals("") ||

(!("cmwap".equals(apnName)||"cmnet".equals(apnName)))) {

return 0;

}

NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (!info.isAvailable()) {

return 0;

}

info = conManager.getActiveNetworkInfo();

String tempAPN = info == null ? "" : info.getExtraInfo();

//if current apn is not cmwap, we have to switch to cmwap.

if (!apnName.equals(tempAPN)) {

if (mNChangeReceiver != null) {

this.unregisterReceiver(mNChangeReceiver);

}

mNChangeReceiver = new NetworkChangeReceiver();

//register receiver for wap network connection.

IntentFilter upIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver(mNChangeReceiver, upIntentFilter);

updateCurrentAPN(getContentResolver(), apnName);

return 1;

}

return 0;

}

在onCreate和onNewIntent的方法中去判斷是否需要切換, 以及設定一些标示。

public void onCreate(Bundle icicle) {

if (!mTabControl.restoreState(icicle)) {

// clear up the thumbnail directory if we can't restore the state as

// none of the files in the directory are referenced any more.

new ClearThumbnails().execute(

mTabControl.getThumbnailDir().listFiles());

// there is no quit on Android. But if we can't restore the state,

// we can treat it as a new Browser, remove the old session cookies.

CookieManager.getInstance().removeSessionCookie();

final Intent intent = getIntent();

final Bundle extra = intent.getExtras();

// Create an initial tab.

// If the intent is ACTION_VIEW and data is not null, the Browser is

// invoked to view the content by another application. In this case,

// the tab will be close when exit.

UrlData urlData = getUrlDataFromIntent(intent, null);

String requestConType = intent.getStringExtra("data_connection");

if (requestConType == null) {

//if launched from launcher, it's treated as WWW browser

if (intent.getAction().equals(Intent.ACTION_MAIN)) {

dataConnectionType = "cmnet";

} else {

//get current APN type and set it to browser

if (info != null) {

String tempAPN = info.getExtraInfo();

dataConnectionType = tempAPN;

}

}

if (forceAPNConnection(dataConnectionType) == 1) {

isSwitchingToNet = true;

}

} else if ("cmwap".equals(requestConType)

&& info != null && (!"cmwap".equals(info.getExtraInfo()))) {

dataConnectionType = "cmwap";

if (forceAPNConnection(dataConnectionType) == 1) {

isSwitchingToWap = true;

}

}

下面會去調用loadUrl和loadDataIn方法, 需要做相應修改:

private void loadUrl(WebView view, String url) {

updateTitleBarForNewLoad(view, url);

if (mNChangeReceiver != null) {

if (BrowserSettings.NAVIGATION_PAGE_URL.equals(url)

|| (url != null && url.startsWith("about:"))) {

autoLoad = true;

view.loadUrl(url);

} else {

autoLoad = false;

mWebView = view;

tempUrl = url;

}

} else {

view.loadUrl(url);

}

}

LoadUrlDataIn方法同時也會被onNewIntent調用。

private void loadUrlDataIn(Tab t, UrlData data) {

//if it's not called from OnCreate and it's switching to wap, postpone data loading. added by Qkf36923

updateTitleBarForNewLoad(t.getWebView(), data.mUrl);

if (mNChangeReceiver == null ||

(data.isEmpty() && mSettings.getHomePage().equals(""))

|| (BrowserSettings.NAVIGATION_PAGE_URL.equals(data.mUrl))

|| (data.mUrl != null && data.mUrl.startsWith("about:"))) {

data.loadIn(t);

} else {

mt = t;

mUrlData = data;

}

}

再看對OnNewIntent的修改

protected void onNewIntent(Intent intent) {

UrlData urlData = getUrlDataFromIntent(intent, current);

//get request connection type, if current connection type is cmwap, it indicates

//this is monternet browser, we won't change APN any more.

String requestConType = intent.getStringExtra("data_connection");

if (requestConType == null) {

//get current APN, set browser connection type.

NetworkInfo network = conManager.getActiveNetworkInfo();

if (network != null && network.isAvailable()) {

dataConnectionType = network.getExtraInfo();

}

} else if ("cmwap".equals(requestConType) || "cmnet".equals(requestConType)){

dataConnectionType = requestConType;

if (forceAPNConnection(dataConnectionType) == 1) {

if ("cmwap".equals(requestConType)) {

isSwitchingToWap = true;

}

if ("cmnet".equals(requestConType)) {

isSwitchingToNet = true;

}

}

}

fromNewIntent = true;

fromNewIntent用來标志是否是第一次建立浏覽器, 這在連接配接上目标APN網絡以後如何打開頁面有關系。

下面是NetworkChangeReceiver的定義

public class NetworkChangeReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {

//NetworkInfo info = conManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

NetworkInfo info = intent.getParcelableExtra(

ConnectivityManager.EXTRA_NETWORK_INFO);

String apn = info.getExtraInfo();

if (((isSwitchingToWap && "cmwap".equals(apn))

|| (isSwitchingToNet && "cmnet".equals(apn)))

&& info.isConnected() && info.isAvailable()) {

if(mNChangeReceiver != null) {

WebView.disablePlatformNotifications();

WebView.enablePlatformNotifications();

if (fromNewIntent) {

loadUrlOnNewIntent();

} else {

if (!autoLoad) {

loadUrlOnCreate();

autoLoad = true;

}

}

BrowserActivity.this.unregisterReceiver(mNChangeReceiver);

mNChangeReceiver = null;

Message message = connectionHandler.obtainMessage(0);

connectionHandler.sendMessageDelayed(message, 300);

}

}

}

}

}

//load data when on Create. added by Qkf36923

private void loadUrlOnCreate() {

if (mUrlData.isEmpty()) {

if (mWebView != null) {

mWebView.loadUrl(tempUrl);

mWebView = null;

}

} else {

if (mUrlData != null) {

mUrlData.loadIn(mt);

mUrlData = null;

}

}

}

private void loadUrlOnNewIntent() {

//updateTitleBarForNewLoad(mt.getWebView(), mUrlData.mUrl);

mUrlData.loadIn(mt);

}

//new handler for setting wap switching flag. added by Qkf36923

Handler connectionHandler = new Handler() {

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (receivedByStateListener) {

receivedByStateListener = false;

resetSwitchingFlag();

} else {

Message message = connectionHandler.obtainMessage(0);

connectionHandler.sendMessageDelayed(message, 300);

}

}

};

connectionHandler用于發送消息重置isSwitchingToWap和isSwitchingToNet标志。 因為本來浏覽器中有一個網絡監聽器, 用于彈出網絡是否存在的提示框。 我們進行接入點切換時網絡會先斷掉, 會被那個監聽器收到, 進而彈出網絡不存在的提示框, 這樣使用者體驗不好,我們需要在切換的時候屏蔽掉。 是以做了以下修改:

mNetworkStateIntentReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(

ConnectivityManager.CONNECTIVITY_ACTION)) {…

if (!isSwitchingToWap && !isSwitchingToNet) {

onNetworkToggle(info.isAvailable());

} else {

這樣當網絡連接配接成功後, 我們需要把标志再重置回去。   這篇文章轉自:  http://seya.iteye.com/blog/966471