Android 之 webView 加载 javascropt
2010-04-14 18:02:00 来源:WEB开发网2. /** Called when the activity is first created. */
3. private WebView mWebView;
4. private Handler mHandler = new Handler();
5.
6. @Override
7. public void onCreate(Bundle savedInstanceState) {
8. super.onCreate(savedInstanceState);
9. setContentView(R.layout.main);
10. mWebView = (WebView) findViewById(R.id.wv1);
11.
12. WebSettings webSettings = mWebView.getSettings();
13. webSettings.setSavePassword(false);
14. webSettings.setSaveFormData(false);
15. webSettings.setJavaScriptEnabled(true);
16. webSettings.setSupportZoom(false);
17.
18. mWebView.setWebChromeClient(new MyWebChromeClient());
19.
20. mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
21.
22. mWebView.loadUrl("file:///android_asset/demo.html");
23. }
24.
25. final class DemoJavaScriptInterface {
26.
27. DemoJavaScriptInterface() {
28. }
29.
30. /**
31. * This is not called on the UI thread. Post a runnable to invoke
32. * loadUrl on the UI thread.
33. */
34. public void clickOnAndroid() {
35.
36. mHandler.post(new Runnable() {
37. public void run() {
38. mWebView.loadUrl("javascript:wave()");
39. }
40. });
41.
42. }
43. }
44.
45. /**
46. * Provides a hook for calling "alert" from javascript. Useful for
47. * debugging your javascript.
48. */
49. final class MyWebChromeClient extends WebChromeClient {
50. @Override
51. public boolean onJsAlert(WebView view, String url, String message,
52. JsResult result) {
更多精彩
赞助商链接