熟悉和掌握Android线程的使用
实验要求:
完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新
分析秒表的计时是否准确,原因是什么,如何保证秒表计时准确
代码实现:
(1)Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="秒表"android:textSize="30dp"android:paddingTop="80dp"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"><TextViewandroid:id="@+id/tv_min"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:text="00"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:text=":"/><TextViewandroid:id="@+id/tv_seconds"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:text="00"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:text=":"/><TextViewandroid:id="@+id/tv_ms"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25dp"android:text="00"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn_clean"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="清零"android:textSize="20dp"/><Buttonandroid:id="@+id/btn_count"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="计时"android:textSize="20dp"/><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="停止"android:textSize="20dp"/></LinearLayout> </LinearLayout>
(2)MainActivity.java文件
package com.example.administrator.myapplication5;import android.app.Activity; import android.content.Intent; import android.os.Handler; import android.os.IBinder; 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 Activity {private static Handler handler=new Handler();private static TextView tv_minus;private static TextView tv_seconds;private static TextView tv_ms;public static int minuts;public static int seconds;public static int mseconds;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_minus=(TextView)findViewById(R.id.tv_min);tv_seconds=(TextView)findViewById(R.id.tv_seconds);tv_ms=(TextView)findViewById(R.id.tv_ms);final Button btn_clean=(Button)findViewById(R.id.btn_clean);final Button btn_count=(Button)findViewById(R.id.btn_count);final Button btn_stop=(Button)findViewById(R.id.btn_stop);final Intent serviceIntent=new Intent(this,CountService.class);btn_clean.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){System.out.println("thisisatestsssss");tv_minus.setText("00");tv_seconds.setText("00");tv_ms.setText("00");}});btn_count.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){startService(serviceIntent);//显示启动service }});btn_stop.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){stopService(serviceIntent);//停止service }});}public static void UpdateGUI(int m,int s,int ms){minuts=m;seconds=s;mseconds=ms;handler.post(RefreshLable);}static Runnable RefreshLable=new Runnable() {@Overridepublic void run() {try {Thread.sleep(50);tv_minus.setText(String.format("%02d",minuts));tv_seconds.setText(String.format("%02d",seconds));tv_ms.setText(String.format("%02d",mseconds%100));} catch (InterruptedException e) {e.printStackTrace();}}}; }
(3)CountService.Java文件
package com.example.administrator.myapplication5;import android.app.Service; import android.content.Intent; //import android.icu.util.Calendar; import android.os.IBinder; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView;import java.util.Calendar;/*** Created by lh on 2018/10/22.*/public class CountService extends Service {private Thread workThread;long startTime=System.currentTimeMillis();@Overridepublic void onCreate() {super.onCreate();workThread=new Thread(null,backgroundThread,"workThread");}@Overridepublic void onStart(Intent intent,int startId) {super.onStart(intent,startId);if(!workThread.isAlive()){workThread.start();}} @Overridepublic void onDestroy() {super.onDestroy();workThread.interrupt();}@Overridepublic IBinder onBind(Intent intent){return null;}private Runnable backgroundThread=new Runnable() {@Overridepublic void run() {try{while(!Thread.interrupted()){long currentTime=System.currentTimeMillis();long time=currentTime-startTime;long m=time/(1000*60);long s=time/1000;long ms=time%1000;int mi=(int)m;int si=(int)s;int msi=(int)ms;MainActivity.UpdateGUI(mi,si,msi);workThread.sleep(50);}}catch (InterruptedException e){e.printStackTrace();}}}; }
(4)AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.administrator.myapplication5"><applicationandroid: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=".CountService"/></application></manifest>
总结:
setText()括号内应该是String类型
System.currentTimeMillis()函数来获取系统时间