Android中webview的DoUpdateVisitedHistory()
来源:互联网 发布:淘宝助理设置橱窗推荐 编辑:程序博客网 时间:2024/06/07 00:58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Webkit;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Test.Droid
{
[Activity(Name = "test.droid.TestWebViewActivity", ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation, Theme = "@style/CustomActivityTheme", AllowTaskReparenting = true)]
public classTestWebViewActivity : BaseActivity,View.IOnTouchListener
{
/// <summary>
/// key
/// </summary>
public const string ExtraPathKey = "path";
public const string FromWhere = "FromWhere";
private WebView webView;
private string url;
private int type;
private bool needClearHistory = false;
public enum FromType
{
Type1 = 0,
Type2 = 1,
Type3 = 2
}
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.LoginWebView);
ActionBar.SetDisplayHomeAsUpEnabled (false);
ActionBar.SetDisplayShowHomeEnabled (true);
ActionBar.SetDisplayUseLogoEnabled (true);
ActionBar.SetHomeButtonEnabled (true);
ActionBar.SetLogo (Resource.Drawable.MenuBackButton);
ActionBar.Title = "";
type = Intent.GetIntExtra (FromWhere, 3);
webView = FindViewById<WebView> (Resource.Id.TestWebView);
webView.Settings.JavaScriptEnabled = true;
JSBridge jsBridge = new JSBridge ();
webView.AddJavascriptInterface (jsBridge, "JSBridge");
TestWebViewClient webViewClient = new TestWebViewClient (this);
webViewClient.LoadingMessage = GetString (Resource.String.common_progress_dialog_message);
webViewClient.ClearHistory += ClearHistory;
webViewClient.OnLoadFinished += OnLoadFinished;
webView.SetWebViewClient (webViewClient);
webView.SetVerticalScrollbarOverlay (true);
webView.SetOnTouchListener (this);
#if DEBUG
webView.SetWebChromeClient (new WebChromeClient ());
#endif
url = Intent.GetStringExtra (ExtraPathKey);
webView.LoadUrl (url);
}
/// <summary>
/// menu项目选择
/// </summary>
public override bool OnOptionsItemSelected (IMenuItem item)
{
switch (item.ItemId) {
case global::Android.Resource.Id.Home:
//当是error画面时,再次点击画面,做刷新操作,此时有一个重定向,使得按下返回按钮时,有history存在,不能直接返回上一个画面。此时做出判 断,若为error画面,则直接finifsh(),返回上一页面
if(webView.CanGoBack () && (webView.Url != GetString (Resource.String.common_webview_error_url)))
{webView.GoBack ();
}else
{
if (type.Equals ((int)FromType.Type1)) {
GATrackEvent ();
} else if (type.Equals ((int)FromType.Type2)) {
GATrackEvent ();
} else {
GATrackEvent ();
}
Finish ();
}break;
}
return true;
}
protected override void OnDestroy ()
{
webView.StopLoading ();
webView.ClearCache (false);
webView.SetWebViewClient (null);
webView.SetWebChromeClient (null);
webView.Destroy ();
base.OnDestroy ();
}
/// <summary>
/// error画面
/// </summary>
/// <param name="v">V.</param>
/// <param name="e">E.</param>
public bool OnTouch (View v, MotionEvent e)
{
if (webView.Url == GetString (Resource.String.common_webview_error_url) && e.Action == MotionEventActions.Up) {
needClearHistory = true;
webView.LoadUrl (url);
return true;
}
return false;
}
public void OnLoadFinished (object sender, ErrorEventArgs e)
{
WebView senderWebView = sender as WebView;
if (e.IsErrorOccurred) {
senderWebView.StopLoading ();
senderWebView.LoadUrl (GetString (Resource.String.common_webview_error_url));
return;
}
if (senderWebView != null) {
if (type.Equals ((int)FromWhereType.SettingLogin)) {
ActionBar.Title = "aaaaa";
GAManager.GetInstance (this).TrackingView ("");
}else{
ActionBar.Title = "bbbbb";
GAManager.GetInstance (this).TrackingView ("");
}
}
}
public void ClearHistory (object sender, EventArgs e)
{
if (needClearHistory) {
needClearHistory = false;
webView.ClearHistory ();
}
}
}
public classTestWebViewClient : WebViewClient
{
private ProgressDialog progressDialog;
public string LoadingMessage { get; set; }
public event EventHandler<EventArgs> OnLoadStarted;
public event EventHandler ClearHistory;
public event EventHandler<ErrorEventArgs> OnLoadFinished;
private Context context;
private bool isErrorOccurred = false;
private bool loadingFinished = true;
private bool redirect = false;
public LoginWebViewClient (Context context)
{
this.context = context;
progressDialog = new ProgressDialog (context);
progressDialog.SetProgressStyle (ProgressDialogStyle.Spinner);
progressDialog.SetCancelable (false);
}
public override void OnPageStarted (WebView view, string url, Android.Graphics.Bitmap favicon)
{
loadingFinished = false;
if (!progressDialog.IsShowing) {
if (LoadingMessage != null) {
progressDialog.SetMessage (LoadingMessage);
progressDialog.Show ();
}
}
if (OnLoadStarted != null) {
OnLoadStarted (view, null);
}
base.OnPageStarted (view, url, favicon);
}
public override void OnPageFinished (WebView view, string url)
{
if(!redirect) {
loadingFinished = true;
}
if(loadingFinished && !redirect){
progressDialog.Dismiss ();
if (OnLoadFinished != null) {
ErrorEventArgs args = new ErrorEventArgs ();
args.IsErrorOccurred = isErrorOccurred;
isErrorOccurred = false;
OnLoadFinished (view, args);
}
} else {
redirect = false;
}
base.OnPageFinished (view, url);
}
/// <summary>
/// error发生时
/// </summary>
/// <param name="view">View.</param>
/// <param name="errorCode">Error code.</param>
/// <param name="description">Description.</param>
/// <param name="failingUrl">Failing URL.</param>
public override void OnReceivedError (WebView view, ClientError errorCode, string description, string failingUrl)
{
isErrorOccurred = true;
base.OnReceivedError (view, errorCode, description, failingUrl);
}
/// <summary>
/// 读入新的url时
/// </summary>
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
if(!loadingFinished){
redirect = true;
}
loadingFinished = false;
if (url.Contains ("testSchema")) {
var intent = new Intent (Intent.ActionView, Android.Net.Uri.Parse (url));
context.StartActivity (intent);
return true;
}
view.LoadUrl (url);
return true;
}
/// <summary>
/// 更新历史记录
/// </summary>
public override void DoUpdateVisitedHistory (WebView view, string url, bool isReload)
{base.DoUpdateVisitedHistory (view, url, isReload);
//清除历史记录
ClearHistory (view, null);
}
}
}
}
- Android中webview的DoUpdateVisitedHistory()
- Android中webview的使用
- android 中webView的使用
- Android中webview的使用
- Android中WebView的使用
- Android 中WebView的使用
- Android中webview的使用
- Android中WebView的使用
- Android 中Webview的难点
- android中webview的使用
- android中WebView 的使用
- android 中WebView的用法
- android中WebView的自适应
- android中webView的设置
- Android中WebView的用法
- Android中WebView的使用方法
- Android中webview的使用
- Android中 "WebView"的使用
- Lintcode:平面列表
- echarts2.0修改markpoint及markline的属性(markpoint显示隐藏,并且实现自定义图例)
- 只需5秒,快速完成电脑大文件复制的技巧
- ORA-01658: 无法为表空间XXX段创建 INITIAL 区
- 如何在Jupyter Notebook中使用Tensorflow
- Android中webview的DoUpdateVisitedHistory()
- C++
- js对浏览器cookie的操作
- 运筹学(4)模式搜索法
- JAVA常用英语
- Android学习笔记之基本程序单元Activity
- Android studio 3.0安装、配置图文教程
- C/C++之美
- 文件切片上传