当国际象棋比赛活动开始时,我想放一个秒表
package com.google.android.webviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.*;
public class WebViewDemo extends Activity {
private static final String LOG_TAG = "Smart Cheekers";
private WebView mWebView;
private Handler mHandler = new Handler();
static boolean check=true;
static int hr=0,min=0,sec=0;
static TextView time;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
time= (TextView) findViewById(R.id.time);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebView.loadUrl("file:///android_asset/index.html");
WebViewDemo.setTime(hr,min,sec);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
while (check == true) {
sec++;
if (sec == 60) {
sec = 0;
min++;
}
if (min == 60) {
min = 0;
hr++;
}
WebViewDemo.setTime(hr,min,sec);
mHandler.postDelayed(this, 100);
}
}
};
/* protected void onPause() {
mHandler.removeCallbacks(mUpdateTimeTask);
hr=0;min=0;
sec=0; check=false;
super.onPause();
}*/
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mUpdateTimeTask, 100);
}
public static void setTime(int hr,int min,int sec){
time.setText(hr+":"+min+":"+sec);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
// item.getItemId();
// Toast.makeText(this, "You pressed the text!",
// Toast.LENGTH_LONG).show();
mWebView.reload();
return super.onOptionsItemSelected(item);
}
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/**
* This is not called on the UI thread. Post a runnable to invoke
* loadUrl on the UI thread.
*/
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:wave()");
}
});
}
public void comBeat() {
Toast.makeText(getApplicationContext(), "You beat me!",
Toast.LENGTH_SHORT).show();
mWebView.reload();
}
public void userBeat() {
Toast.makeText(getApplicationContext(), "Gotcha! Game over.",
Toast.LENGTH_SHORT).show();
mWebView.reload();
}
}
/**
* Provides a hook for calling "alert" from javascript. Useful for debugging
* your javascript.
*/
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
Log.d(LOG_TAG, message);
result.confirm();
return true;
}
}
}