`
seya
  • 浏览: 355847 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Android浏览器Browser二次开发(四)浏览器中的APN切换

阅读更多
第四章 浏览器中的APN切换
业务需求:有些链接需要使用CMWAP接入点才能成功访问, 我们的浏览器必须能够自动切换CNNET, CMWAP.
分析:调用浏览器的时候在Intent中传递一个参数, 用于标示接入点需要。 我们的浏览器需要处理3种情况, 第一是参数为cmwap时, 切换成cmwap; 第二是参数为cmnet或者从桌面上的浏览器图标点击, 第一次启动浏览器时, 切换成cmnet; 第三是打开一个链接, 没有参数时, 不做切换, 直接使用当前接入点。 如果已经打开浏览器, 并且切换成cmwap, 无论后来如何去手动设置接入点, 当点击浏览器里面的链接, 或者输入其他地址进行访问时, 必须能够再次切换成wap. 已经打开为cmnet浏览器同理。 在需要切换的时候, 必须等到切换完成, 新的接入点成功连接上以后才能开始数据的载入。
改动: 1. 接入点切换方法
/*
* change current network connection to cmwap if it's not. added by Qkf36923
*/
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);
/*
* when request connection is specified to cmwap, we have to change current APN
* added by Qkf36923, 20110113
*/
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 url is navigation page, we can load it from local directly.
* Added by QuYueting 20110310. Start.
*/
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);
}
/**Added by QuYueting 20110310. End**/
}
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 no need to switch or home page is set as empty, we should load navigation page instantly.
* Added by QuYueting 20110310. Start**/
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;
}
/**if no need to switch or home page is set as empty, we should load navigation page instantly.
* Added by QuYueting 20110310. End**/
}

再看对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的定义
/*
* receiver for observing network connection change, added by Qkf36923
*/
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()) {
/*
* apn change message is sent out more than once during a second, but it
* only happens once practically.
*/
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 {

这样当网络连接成功后, 我们需要把标志再重置回去。

分享到:
评论
1 楼 xuhl1022 2012-11-09  
看了您的android 4篇 浏览器二次开发,感触颇深,写的很精彩,同时我又有几个问题,想请教一下:第一:怎么给内置浏览器增加一个导航的主页?第二:我将内置浏览器,独立的编译出来了,但是只能在特定的sdk版本中运行,比如:我使用Android 2.3.3版本的sdk编译的apk,它只能在android 2.3.3模拟器里运行,其他的任何版本都不可以。我想让我的浏览器支持多个版本的sdk该怎么做?

相关推荐

Global site tag (gtag.js) - Google Analytics