android Js调用本地代码不识别

在小米手机上测试demo,发现js方法不识别,经过查找发现4.4.2以上js方法前需要加 @JavascriptInterface 使用此语句时SDK环境需要是4.4.2或更高。

package com.joanzapata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.joanzapata.pdfview.sample.R;

public class HtmlActivity extends Activity {
	WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.html);
		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);

		webView.addJavascriptInterface(new JsInterface(), "JSINTERFACE");
		webView.loadUrl("file:///android_asset/pdf.html");
	}

	public final class JsInterface {
		@JavascriptInterface
		public void goPdf() {
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Intent intent = new Intent(HtmlActivity.this,
							PDFViewActivity.class);
					HtmlActivity.this.startActivity(intent);

				}
			});
		}
	}
}


posted @ 2017-01-09 11:42  清澈见底  阅读(241)  评论(0编辑  收藏  举报