PowenKo, Android Tutorial 054-3, internet, WebView load local html and call javascript function.

September 3, 2011   ·   0 Comments

PowenKo, Android Tutorial 054-2, internet, WebView load local html and call javascript function.



package com.powenko.Tutorial_UI_WebView_local_HTML;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Tutorial_UI_WebView_local_HTMLActivity extends Activity {

	private Handler mHandler;
	private WebView webview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
   
        webview = (WebView) findViewById(R.id.webView1);  
        webview.getSettings().setJavaScriptEnabled(true);
       
        webview.setWebViewClient(new HelloWebViewClient());
        
        
     
        mHandler=new  Handler();
       
        webview.addJavascriptInterface(new Object() {  
            public void clickOnAndroid() {  
                mHandler.post(new Runnable() {  
                    public void run() {  
                    	webview.loadUrl("javascript:wave()");  
                    }  
                });  
            }  
        }, "demo");  
        webview.loadUrl("file:///android_asset/demo.html");  

    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <android.webkit.WebView android:id="@+id/webView1" android:layout_width="fill_parent" android:layout_height="fill_parent"></android.webkit.WebView>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.powenko.Tutorial_UI_WebView_local_HTML"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Tutorial_UI_WebView_local_HTMLActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

demo.html

<html>  
        <script language="javascript">  
                function wave() {  
                    document.getElementById("droid").src="http://www.powenko.com/en/wp-content/uploads/2011/08/Powen_Ko-copy-300x199.jpg";  
                }  
        </script>  
        <body>  
            <a onClick="window.demo.clickOnAndroid()">  
                                <img id="droid" src="http://www.powenko.com/en/wp-content/uploads/2011/08/icon1.png"/><br>  
                                Click me!  
            </a>  
        </body>  
</html>
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon