package com.example.mywork;
import java.util.Timer;
import java.util.TimerTask;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.os.Build;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView webView;
private static boolean mBackKeyPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.initWebView();
}
protected void initWebView(){
this.webView = (WebView)findViewById(R.id.webView);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setWebViewClient(new WebViewClient());
this.webView.loadUrl("http://www.1tamche.com");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if(this.webView.canGoBack()){
this.webView.goBack();
return true;
}else{
if(!mBackKeyPressed){
mBackKeyPressed = true;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mBackKeyPressed = false;
}
}, 2000);
return true;
}
else{
this.finish();
System.exit(0);
}
}
}
return super.onKeyDown(keyCode,event);
}
}
class webViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}