业务需求:有些链接需要使用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 {
这样当网络连接成功后, 我们需要把标志再重置回去。
android 浏览器APN切换
最新推荐文章于 2020-12-20 15:05:30 发布