Android Open Source - AndroidWebViewSample Main Activity






From Project

Back to project page AndroidWebViewSample.

License

The source code is released under:

MIT License

If you think the Android project AndroidWebViewSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tatuas.android.webviewsample;
/*from ww  w  .j a  v a2 s .  co  m*/
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final WebView wv = (WebView) findViewById(R.id.webView1);
        final String url = "http://www.google.co.jp";

        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                // LogCat???????ID???
                Log.w("WebViewSample", "loading...");
                Log.w("webViewSample",
                        "WebView thread Id = "
                                + Long.toString(Thread.currentThread().getId()));
            }
        });

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        wv.getSettings().setJavaScriptEnabled(true);

        Button normalBtn = (Button) findViewById(R.id.execNormalBtn);
        normalBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("Activity", "Start!");
                Log.w("Activity", "Activity thread id = "
                        + Thread.currentThread().getId());

                Log.w("Activity", "threadId" + Thread.currentThread().getId());
                wv.loadUrl(url);
            }
        });

        Button uiThreadBtn = (Button) findViewById(R.id.execUIThreadBtn);
        uiThreadBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("Activity", "Start!");
                new Thread(new Runnable() {
                    public void run() {
                        Log.w("UiThread", "UiThread thread id = " +
                                Thread.currentThread().getId());
                        runOnUiThread(new Runnable() {
                            public void run() {
                                Log.w("UiThread", "uithread webview id = " +
                                        Thread.currentThread().getId());
                                wv.loadUrl(url);
                            }
                        });
                    }
                }).start();
            }
        });

        Button handlerBtn = (Button) findViewById(R.id.execHandlerBtn);
        handlerBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("Activity", "Start!");
                final Handler handler = new Handler();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.w("Handler", "handler thread id = " +
                                Thread.currentThread().getId());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Log.w("handler", "handler webview id = " +
                                        Thread.currentThread().getId());
                                wv.loadUrl(url);
                            }
                        });
                    }
                }).start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}




Java Source Code List

com.tatuas.android.webviewsample.MainActivity.java