现在的位置: 首页 > 综合 > 正文

android.webkit.WebView 体验2:概括

2013年09月11日 ⁄ 综合 ⁄ 共 4144字 ⁄ 字号 评论关闭

我前面已经对webview进行了讲说,但是今天想看看文档到底怎么说的。(不过,可惜的是,我英语确实太烂了)

 

 

 

 请注意,为了使您的活动来访问互联网和加载WebView加载网页,您必须添加到你的Android互联网权限的文件清单

这必须是一个孩子的<manifest>元素。

意思应该就是必须添加这个权限才可以使用webview加载网页。但是到底是什么意思?我前面用的时候就没有加这个权限,照样使用。

难道是,不加这个不能访问互联网还是?

我在网页里面添加了:

<a href="http://www.baidu.com">访问百度</a>

也是可以的,那就不知道,呵呵

一:直接显示某网站地址

public class MainActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webview = new WebView(this);
        setContentView(webview);
        // Simplest usage: note that an exception will NOT be thrown
        // if there is an error loading this page (see below).
        webview.loadUrl("http://slashdot.org/");

显示:Web page not available。。。。。。

那我们加上上面说到的权限试试(<uses-permission android:name="android.permission.INTERNET" />

OK,通过

二:如果我们只想访问很简单的页面,但是也不想从新创建html怎么办?

String summary = "<html><body>You scored <b>192 points.</body></html>";
webview.loadData(summary, "text/html", "utf-8");

需要注意的是,上面两点就不能如我们上篇文章讲的那样进行交互了

三:那么如何让java和js进行交互

http://blog.csdn.net/partner4java/archive/2010/04/05/5452613.aspx

这里面有,我就不多说了

然后还有两种交互场景:

Here's a more complicated example, showing error handling, settings, and progress notification:

 // Let's display the progress in the activity title bar, like the
 
// browser app does.
 getWindow
().requestFeature(Window.FEATURE_PROGRESS);

 webview
.getSettings().setJavaScriptEnabled(true);

 
final Activity activity = this;
 webview
.setWebChromeClient(new WebChromeClient() {
   
public void onProgressChanged(WebView view, int progress) {
     
// Activities and WebViews measure progress with different scales.
     
// The progress meter will automatically disappear when we reach 100%
     activity
.setProgress(progress * 1000);
   
}
 
});
 webview
.setWebViewClient(new WebViewClient() {
   
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   
}
 
});

 webview
.loadUrl(http://slashdot.org/);

大体就是发生js错误和访问进展之类的,反正没看懂

四:cookie管理


Note that, in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:

<uses-permission android:name="android.permission.INTERNET" />

This must be a child of the <manifest> element.

 

 

我做了下面的一个例子试了一下

 

package cn.partner4java.action;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
 private WebView webView;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  webView = (WebView) this.findViewById(R.id.webView);
  //webView.loadUrl("file:///android_asset/index.html");
  webView.loadUrl("http://www.baidu.com");
  //requestFocus(int direction, Rect previouslyFocusedRect)
  //Call this to try to give focus to a specific view or to one of its descendants and give it hints about the direction and a specific rectangle that the focus is coming from. Looks for a view to give focus to respecting the setting specified by getDescendantFocusability().
  webView.requestFocus();
  
  //Set the WebViewClient that will receive various notifications and requests.
  webView.setWebViewClient(new WebViewClient(){
   String loginCookie = "";
   @Override
   public void onLoadResource(WebView view, String url) {
    CookieManager cookieManager = CookieManager.getInstance();
    loginCookie = cookieManager.getCookie(url);
    cookieManager.setCookie("name", "王昌龙");
    Toast.makeText(MainActivity.this, loginCookie, 0).show();
    Toast.makeText(MainActivity.this, url, 0).show();
   }

   @Override
   public void onPageFinished(WebView view, String url) {
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setCookie(url, loginCookie);
    Toast.makeText(MainActivity.this, "onPageFinished:" + loginCookie, 0).show();
    Toast.makeText(MainActivity.this,"onPageFinished:" + url, 0).show();
    Toast.makeText(MainActivity.this,"name:" + cookieManager.getCookie("name"), 0).show();
    
   }
   
  });
 }

}

 

 

 当:webView.loadUrl("file:///android_asset/index.html");

时。所有的toast只有url有东西,我本以为我set了那么,会获取到我的设置值

 

当:webView.loadUrl("http://www.baidu.com");

根据那个url获取到了cookie值,但是我设置的也是不起作用,用j2ee的思路去想,我自己设置个cookie,告诉百度你根绝我的要求建立了个cookie值,估计他也是不乐意的。

 

再深层次的理解,我也没那么大脑袋了,等以后碰到了,再补这一块吧

 

 

 

抱歉!评论已关闭.