android 中 webview 怎么用 localStorage

android 中 webview 怎么用 localStorage,第1张

应该是public void showSource(String html) {} 运行在非UI线程里面, 你尝试在这个方法里面使用handlersendMessage() 然后在handler的handlerMessage 方法中更新TextView中的内容试试

请求的url记录是保存在webviewCachedb,而url的内容是保存在webviewCache文件夹下

为了便于理解,接下来模拟一个案例,定义一个html文件,在里面显示一张,用WebView加载出来,然后再试着从缓存里把这张读取出来并显示。

第一步:新建一个Android工程命名为WebViewCache目录结构如下:

第二步:在assets目录下新建一个html文件,命名为indexhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">

<html>

<head>

<title>WebViewCacheDemo</title>

<meta >

The extensive Android SDK allows you to do many great things with particular views like the WebView for displaying webpages on Android powered devices

Android SDK 的扩展通过使用特定的view允许你做许多事情比如WebView用来在Android手机上展示网页

As of lately while I was experimenting with the Android SDK I was using a WebView in one of my activities

最近我在体验Android SDK的时候在一个Activity中用到了WebView

From that particular WebView I needed to know the ContentHeight but also the ContentWidth

从WebView我不但想要知道ContentHeight还想知道ContentWidth

Now getting the contentHeight is easy like so:

现在的情况是获取contentHeight很easy如下

webviewgetContentHeight();

Unfortunately getting the contentWidth from a WebView is rather more difficult since there is not a simple method like:

不幸的是从一个WebView获取contentWidth是相当困难因为SDK中没有一个像这样的方法

// THIS METHOD DOES NOT EXIST!

webviewgetContentWidth();

There are ways to get the contentWidth of the rendered HTML page and that is through Javascript If Javascript can get it for you then you can also have them in your Java code within your Android App

当然是有方法获取contentWidth的就是通过Javascript来获取如果你能够支持Javascript那么你就可以在你的Android 程序中使用java代码来获取宽度

By using a JavascriptInterface with your WebView you can let Javascript communicate with your Android App Java code by invoking methods on a registered object that you can embed using the JavascriptInterface

通过在你的WebView中使用JavascriptInterface通过调用你注册的JavascriptInterface方法可以让Javascript和你的Android程序的java代码相互连通

So how does this work

怎么做呢?

For a quick example I created a simple Activity displaying a webview that loads a webpage wich displays a log message and a Toast message with the contentWidth wich was determined using Javascript Note that this happens AFTER the page was finished loading because before the page is finished loading the width might not be fully rendered Also keep in mind that if there is content loaded asynchronously that it doesnt affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a % width webpage)

搭建一个快速的例子创建一个简单的展示webView的Activity一个LogCat消息一个Toast消息用来显示我们通过 Javascript获取的宽度注意这些会在网页完全加载之后显示因为在网页加载完成之前宽度可能不能够正确的获取到同时也要注意到如果是异 步加载这并不影响宽度(最多高度会受影响因为宽度总是在CSS文件中做了完全的定义除非在网页中你用了%宽度)

Below is the code of the Activity Mainjava:

下面的代码是Activity的代码

package compimmosandroidsampleswebviewcontentwidth;

import androidappActivity;

import androidosBundle;

import androidutilLog;

import androidwebkitWebView;

import androidwebkitWebViewClient;

import androidwidgetToast;

publicclass Main extends Activity {

privatefinalstatic String LOG_TAG = "WebViewContentWidth";

privatefinal Activity activity = this;

privatestaticint webviewContentWidth = ;

privatestatic WebView webview;

/ Called when the activity is first created /

@Override

publicvoid onCreate(Bundle savedInstanceState) {

superonCreate(savedInstanceState);

setContentView(Rlayoutmain);

webview = (WebView) findViewById(Ridwebview);

webviewgetSettings()setJavaScriptEnabled(true);

webviewsetSaveEnabled(true);

webviewaddJavascriptInterface(new JavaScriptInterface() "HTMLOUT");

webviewsetWebViewClient(new WebViewClient() {

@Override

publicvoid onPageFinished(WebView view String url) {

webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName(html)[]scrollWidth);");

}

});

webviewloadUrl(";);

}

class JavaScriptInterface {

publicvoid getContentWidth(String value) {

if (value != null) {

webviewContentWidth = IntegerparseInt(value);

Logd(LOG_TAG "Result from javascript: " + webviewContentWidth);

ToastmakeText( activity

"ContentWidth of webpage is: " +

webviewContentWidth +

"px" ToastLENGTH_SHORT)show();

}

}

}

}

Below is the XML layout used with the Activity wich only contains a simple WebView:

下面是Activity的Layout主要就是一个简单的WebView

<xml version="" encoding="utf">

<LinearLayout

xmlns:android=";

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<WebView android:id="@+id/webview"

android:layout_width="fill_parent"

android:layout_height="fill_parent" />

</LinearLayout>

AndroidManifestxml layout:

AndroidManifestxml代码

<xml version="" encoding="utf">

<manifest

xmlns:android=";

package="compimmosandroidsampleswebviewcontentwidth"

android:versionCode="" android:versionName="">

<application android:icon="@drawable/icon"

android:label="@string/app_name">

<activity android:name="Main"

android:label="@string/app_name">

<intentfilter>

<action android:name="androidintentactionMAIN" />

<category

android:name="androidintentcategoryLAUNCHER" />

</intentfilter>

</activity>

</application>

<usessdk android:minSdkVersion="" />

<usespermission android:name="androidpermissionINTERNET" />

</manifest>

You can also download the full source of Android Application WebViewContentWidth!

1、webViewloadUrl();直接显示网页内容(单独显示网络),一般不会出现乱码。2、webViewloadData(data, "text/html", "UTF-8");loadData主要被设计用来装载URI格式的数据,它不能通过网络来加载内容。网上流传的webview加载中文出现乱码,多数是使用此方法。使用过程中主要有两个问题:(1)loadData不能加载内容,如果想加载内容或者获得更强大的Web支持建议使用更强大的loadDataWithBaseURL(2) 许多实用loadData方法的朋友都遇到显示乱码的问题,那是因为编码器设置错误导致的。我们知道String类型的数据主要是unicode编码,而WebView一般为了节省资源使用的是UTF-8编码,所以我们在loadData的时候要告诉方法怎样转码。即要告诉它要将unicode编码的内容转成UTF-8编码的内容。有些朋友虽然在loadData的时候设置了编码方式,但是还是显示乱码,这是因为还需要为WebView的text编码指定编码方式。举例如下:WebView wv = (WebView)findViewById(Ridwebview) ;wvgetSettings()setDefaultTextEncodingName(“UTF -8”) ;wvloadData(content, “text/html”, “UTF-8”) ;注意为gb2312或gbk(3)网页说明编码格式 以上两种方法是网上给的比较好的方法,但是我都试了下都没有解决我的乱码问题。 原来我是用LoadData方法来解析html的,但是据说这是官方的一个BUG,不能用来解析中文。所以绕其道而行之,采用loadDataWithBaseURL的方法,其中codeingType设置为utf-8就OK了。3、loadDataWithBaseURL如果单纯显示文字的话可以写webViewloadDataWithBaseURL(null, string, "text/html", "utf-8", null);如果要显示可以写webViewloadDataWithBaseURL(baseUrl, string, "text/html", "utf-8", null);其中baseUrl为你存储照片的路径,比如:

现在越来越多的APP都内置了Web网页去加载视图,也就是我们常说的Hybrid APP混合应用,市场上很多平台都是这样做的,比如我们经常使用的淘宝、京东等电商平台。这些是如何实现的呢?在我们Android中有一个WebView组件,它就可以实现此类功能。它是Android中的原生UI控件,主要用于在APP应用中方便地访问远程网页或本地HTML资源,同时WebView也在Android中充当Java代码和JS代码之间交互的桥梁,实际上也可以将WebView看做一个功能最小化的浏览器。下面我们一起来看看WebView的一些常用的使用方法。

WebView是Android系统提供的一个能显示网页的系统控件,它是一个特殊的View,同时也是一个ViewGroup,可以有很多其他子View。在Android 44以下(不包含44)系统WebView底层实现是采用WebKit内核,而在Android 44及其以上Google采用了Chromium内核作为系统WebView的底层内核支持。在这一变化中Android提供的WebView相关API并没有发生较大变化,在44上也兼容低版本的API并且引进了少部分API。这里简单介绍下基于Chromium的WebView和基于WebKit的WebView之间的差异,基于Chromium的Webview提供了更广的HTML5、CSS3、JavaScript支持,在Android系统版本50上基于Chromium 37,WebView提供了绝大多数的HTML5特性支持,除此之外Chromium也支持远程调试(Chrome DevTools)。WebKit JavaScript引起采用WebCore JavaScript在Android 44上换成了V8能直接提升JavaScript性能。

二、作用

1、显示和渲染Web页面

2、使用html文件(网络上或本地assets中)作为布局

3、可与JavaScript交互调用

注: WebView控件功能强大,除了具有一般View的属性和设置外,还可以对Url请求、页面加载、渲染、页面交互进行强大的处理。

以上就是关于android 中 webview 怎么用 localStorage全部的内容,包括:android 中 webview 怎么用 localStorage、android webView怎么得到当前地址、Android 在WebView中通过javascript获取网页源码,并在TextView或者在EditText中显示问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9316396.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存