利用Android中的三大主件来实现一个码表
Android中的四大组件大家都知道吧!
Activity、Service、Broadcast Receiver、Content Provider
所以我们这里会用到前三大组件:
//这是service类
package cn.zuxia.andorid.app;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
public class MyService extends Service {
private static String values;
private static boolean flag=true;
//定义四个整型变量,用来存储时分秒毫秒等
private int mHour = 0, mMinute = 0, mSecond = 0, mMs = 0;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//这里我们是返回一个空。当然,如果我们要绑定服务,可以在这里发返回一个服务
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
/**
* 重写的start方法,用来执行一系列操作
*/
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//得到传入的值
values = intent.getStringExtra("values");
//如果传入的是start,也就是开始计时按钮,就进入
if ("start".equals(values)) {
flag=true;
}
//如果传入的值是pause,也就是暂停,那么在这里面将要执行暂停操作
else if("pause".equals(values)){
flag=false;
}else if ("continue".equals(values)) {
flag=true;
}
//启用一个线程,用来刷新时间
new Thread(new Runnable() {
@Override
public void run() {
//暂停时就进入这个循环,循环条件是根据传入的值,也就是按下的按钮进行处理的
while (!flag) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//启用一个while循环
while (flag) {
try {
//休眠10毫秒,然后刷新
Thread.sleep(10);
mMs++;
//获取一个message实例,传入数据
Message message = mHandler.obtainMessage();
//定义一个messageId
message.what = 1;
//发送信息
mHandler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (1 == msg.what) {
//定义四个String,主要用于修饰界面美观,也就是说用来填充显示是的空位,之后在if语句中赋值
String value1 = "", value2 = "", value3 = "", value4 = "";
//应用一系列的if判断,用来操作时分秒的计算与赋值
if (mMs >= 99) {
mMs = 0;
mSecond++;
}
if (mSecond > 59) {
mSecond = 0;
mMinute++;
}
if (mMinute > 59) {
mMinute = 0;
mHour++;
}
//如果都是一位数,那么就在其前面加一个0,填充
if (mMs < 10) {
value1 = "0";
}
if (mSecond < 10) {
value2 = "0";
}
if (mMinute < 10) {
value3 = "0";
}
if (mHour < 10) {
value4 = "0";
}
//打印显示数据
android_stop_watchActivity.mTextView.setText("第一个码表:" + value4
+ mHour + ":" + value3 + mMinute + ":" + value2
+ mSecond + ":" + value1 + mMs);
}
}
};
}
//这是activity类
package cn.zuxia.andorid.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class android_stop_watchActivity extends Activity {
//声明两个button控件
private Button buttonStart,buttonPause;
//声明一个TextView控件,用于显示码表信息
public static TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取TextView实例
mTextView=(TextView) findViewById(R.id.displayFirstInfo);
buttonStart=(Button) findViewById(R.id.startBtn);
buttonPause=(Button) findViewById(R.id.pauseBtn);
}
/**
* 实现单击按钮监听事件方法
* @param v
*/
public void onAction(View v){
//创建Intent对象
Intent intent=new Intent();
if (v.getId() == R.id.startBtn) {
startService(intent,"start");
buttonStart.setEnabled(false);
} else if (v.getId() == R.id.pauseBtn) {
String text=buttonPause.getText().toString();
if ("暂停".equals(text)) {
startService(intent,"pause");
buttonPause.setText("继续");
}else{
startService(intent,"continue");
buttonPause.setText("暂停");
}
} else if (v.getId() == R.id.stopBtn) {
stopService(intent);
buttonStart.setEnabled(true);
}
}
/**
* 创建一个启动服务方法
*/
private void startService(Intent intent,String values){
intent.setClass(android_stop_watchActivity.this, MyService.class);
intent.putExtra("values", values);
startService(intent);
}
}
//这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/displayText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center" />
<Button
android:id="@+id/startBtn"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/displayText"
android:layout_margin="4dip"
android:onClick="onAction"
android:text="开始"
/>
<Button
android:id="@+id/pauseBtn"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/displayText"
android:layout_toRightOf="@id/startBtn"
android:layout_margin="4dip"
android:onClick="onAction"
android:text="暂停"
/>
<Button
android:id="@+id/stopBtn"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/displayText"
android:layout_toRightOf="@id/pauseBtn"
android:layout_margin="4dip"
android:onClick="onAction"
android:text="停止"
/>
<TextView
android:id="@+id/displayFirstInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pauseBtn"
android:text="第一个码表:00:00:00:00"
android:gravity="center"
/>
</RelativeLayout>