package com.javacodegeeks.android.androidintentexample;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static int MSG_ID = 2;
	private Handler handler;
	final Context context = this;
	private WebView webView;
	private String content;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);

		handler = new Handler() {

			@Override
			public void handleMessage(Message message) {

				if (message.arg1 == MSG_ID) {

					content = (String) message.obj;

					this.post(new Runnable() {

						public void run() {

							webView.loadData(content, "text/html; charset=UTF-8", null);
							
						}

					});

				}
			}
		};

	}

	public void readWebpage(View view) {
		
		Intent intent = new Intent(this, LoadWebPageService.class);

		Messenger messenger = new Messenger(handler);
		intent.putExtra("MESSENGER", messenger);
		intent.putExtra("URL", "http://examples.javacodegeeks.com");
		startService(intent);

	}
	
public void launchbrowser(View view) {
		
	Intent intentImplicit = new Intent(Intent.ACTION_VIEW, Uri.parse("http://examples.javacodegeeks.com"));
    try { 
          startActivity(intentImplicit);
        } catch (ActivityNotFoundException ex) { 
            ex.printStackTrace();
        }

	}
}