實驗五 使用執行緒(系統時間校驗版)
阿新 • • 發佈:2019-01-07
實驗五 使用執行緒
實驗目的
熟悉和掌握Android執行緒的使用
實驗要求
- 完成一個秒錶,具備啟停功能,正確使用工作執行緒完成介面重新整理
- 分析秒錶的計時是否準確,原因是什麼,如何保證秒錶計時準確
工程程式碼
專案工程結構圖
MainActivity:
package com.example.wyx.exp_52;
import android.app.Activity;
import android.content. Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static Handler handle = new Handler ();
private static long third;
static TextView hour;
static TextView minute;
static TextView miao;
Button start;
Button stop;
Button qingling;
public static void Update(long ms){
third=ms;
handle.post(RefreshLable);
}
private static Runnable RefreshLable= new Runnable(){
@Override
public void run(){
//hour.setText(String.valueOf(first));
//minute.setText(String.valueOf(second));4
//miao.setText(String.valueOf(third));
if(third<6000)
{
if(third<100)
miao.setText(Long.toString(third));
else{
miao.setText(Long.toString(third%100));
minute.setText(Long.toString(third/100));
}
//數字轉換字串
}
else
{
hour.setText(Long.toString(third/6000));
minute.setText(Long.toString((third%6000)/100));
miao.setText(Long.toString((third%6000)%100));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hour=(TextView)findViewById(R.id.textView2);
minute=(TextView)findViewById(R.id.textView4);
miao=(TextView)findViewById(R.id.textView6);
qingling=(Button)findViewById(R.id.button4);
start=(Button)findViewById(R.id.button5);
stop=(Button)findViewById(R.id.button6);
final Intent serviceIntent=new Intent(this,MainService.class);
qingling.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(serviceIntent);
hour.setText("00");
minute.setText("00");
miao.setText("00");
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(serviceIntent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(serviceIntent);
}
});
}
}
MainService:
package com.example.wyx.exp_52;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.Calendar;
import java.util.Date;
public class MainService extends Service {
private Thread workthread;
private long ms,ms0;
private int ts1,ts2;
private boolean isstart=false;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public void onCreate() {
super.onCreate();
workthread = new Thread(null,backgroundWork,"workthread");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(!workthread.isAlive()){
workthread.start();
/*Calendar cal=Calendar.getInstance();
min=cal.get(Calendar.MINUTE);
s=cal.get(Calendar.SECOND);
ms=cal.get(Calendar.MILLISECOND);*/
ms=System.currentTimeMillis();
}
}
@Override
public void onDestroy() {
super.onDestroy();
workthread.interrupt();
}
private Runnable backgroundWork =new Runnable() {
@Override
public void run() {
try {
while(!workthread.interrupted())
{
// Calendar cal=Calendar.getInstance();
//int min0=Math.abs(cal.get(Calendar.MINUTE)-min);
//int s0=Math.abs(cal.get(Calendar.SECOND)-s);
//int ms0=Math.abs(cal.get(Calendar.MILLISECOND)-ms)/10;
ms0=(System.currentTimeMillis()-ms)/10;
/*if(s0!=ts2) {
ts1 = ts2;
ts2 = s0;
if(ts2<ts1){
ts1=ts1;
}
}*/
//int ms0=Math.abs(cal.get(Calendar.MILLISECOND)-ms)/10;
MainActivity.Update(ms0);
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
/*
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!workthread.isAlive()) {
workthread.start();
Calendar cal = Calendar.getInstance();
min = cal.get(Calendar.MINUTE);
s = cal.get(Calendar.SECOND);
ms = cal.get(Calendar.MILLISECOND);
}
return START_STICKY;
}*/
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="秒錶"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_toStartOf="@+id/textView3"
android:text="00"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="00"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_toEndOf="@+id/textView5"
android:text="00"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toStartOf="@+id/textView4"
android:text=":"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toEndOf="@+id/textView4"
android:text=":"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="230dp"
android:layout_height="60dp"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:orientation="horizontal">
<Button
android:id="@+id/button4"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="清零"
android:textSize="20dp"/>
<Button
android:id="@+id/button5"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="計時"
android:textSize="20dp"/>
<Button
android:id="@+id/button6"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="停止"
android:textSize="20dp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wyx.exp_52">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MainService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
實現效果