你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中Webview使用自定义的JavaScript进行回调

[日期:2012-01-19] 来源:Linux社区  作者:arui319 [字体:大 中 小]

使javascript功能可用。这部分代码都放在UserListActivity中的onCreate()方法里。

然后是注册JS接口。先看看webview的一个方法。

public void addJavascriptInterface (Object obj, String interfaceName)

Since: API Level 1

Use this function to bind an object to JavaScript so that the methods can be accessed from JavaScript.

IMPORTANT:

·         Using addJavascriptInterface() allows JavaScript to control your application. This can be a very useful feature or a dangerous security issue. When the HTML in the WebView is untrustworthy (for example, part or all of the HTML is provided by some person or process), then an attacker could inject HTML that will execute your code and possibly any code of the attacker's choosing.
Do not use addJavascriptInterface() unless all of the HTML in this WebView was written by you.

·         The Java object that is bound runs in another thread and not in the thread that it was constructed in.

Parameters

obj

The class instance to bind to JavaScript, null instances are ignored.

interfaceName

The name to used to expose the instance in JavaScript.

 

我们在UserListActivity类的onCreate()方法中增加如下语句:

mWebView.addJavascriptInterface(this, "Android");

在UserListActivity类中增加如下方法:

public void user(String id) {

        // 获取id,跳转activity。

    }

 

这样当页面调用onclick="javascript:android.user('1')"语句的时候,就可以映射到UserListActivity对象的user()方法了。

这里user方法有一个参数,是要对应js语句的user(‘1’)。

下面附上所有代码。

Android部分的代码: 

 
  1. package com.arui.framework.android.js;  
  2.   
  3.    
  4.   
  5. import android.app.Activity;  
  6.   
  7. import android.content.Intent;  
  8.   
  9. import android.os.Bundle;  
  10.   
  11. import android.view.View;  
  12.   
  13. import android.webkit.WebSettings;  
  14.   
  15. import android.webkit.WebView;  
  16.   
  17.    
  18.   
  19. import com.arui.framework.R;  
  20.   
  21. import com.arui.framework.android.js.UserDetailActivity;  
  22.   
  23.    
  24.   
  25. public class UserListActivity extends Activity {  
  26.   
  27.    
  28.   
  29.     private WebView mWebView;  
  30.   
  31.       
  32.   
  33.     @Override  
  34.   
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.   
  37.    
  38.   
  39.         super.onCreate(savedInstanceState);  
  40.   
  41.          
  42.   
  43.        setContentView(R.id.userlist);  
  44.   
  45.          
  46.   
  47.        mWebView = (WebView) findViewById(R.id.mywebview);  
  48.   
  49.        WebSettings webSetting = mWebView.getSettings();  
  50.   
  51.        //设置js可用   
  52.   
  53.        webSetting.setJavaScriptEnabled(true);  
  54.   
  55.        // 添加js调用接口   
  56.   
  57.        mWebView.addJavascriptInterface(this, "android");   
  58.   
  59.        //载入具体的web地址   
  60.   
  61.        mWebView.loadUrl("http://www.linuxidc.com/topicnews.aspx?tid=11");  
  62.   
  63.        mWebView.setVisibility(View.VISIBLE);  
  64.   
  65.        mWebView.requestFocus();  
  66.   
  67.     }  
  68.   
  69.       
  70.   
  71.     public void user(String id) {   
  72.   
  73.        //跳转activity   
  74.   
  75.        Intent intent = new Intent(this, UserDetailActivity.class);  
  76.   
  77.        intent.putExtra("id", id);  
  78.   
  79.        startActivity(intent);  
  80.   
  81.     }   
  82.   
  83. }  

资源文件:

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout   
  4.   
  5.     xmlns:android="http://schemas.android.com/apk/res/android"  
  6.   
  7.     android:orientation="vertical"  
  8.   
  9.     android:layout_width="fill_parent"  
  10.   
  11.     android:layout_height="fill_parent" >  
  12.   
  13.     <WebView  
  14.   
  15.        android:id="@+id/mywebview"  
  16.   
  17.        android:layout_width="fill_parent"  
  18.   
  19.        android:layout_height="fill_parent"   
  20.   
  21.        android:visibility="gone"/>  
  22.   
  23. </LinearLayout>  

Web页面的局部代码:

<img src="……" onclick="javascript:android.user('1')" />

linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款