Android实验5:使用线程

1. 完成一个秒表,具备启停功能,正确使用工作线程完成界面刷新

2. 分析秒表的计时是否准确,原因是什么,如何保证秒表计时准确


MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{

    private static Handler handle = new Handler();
    private static TextView textview = null;
    private static String str1;

    public static void UpdateGUI(String string){
        str1 = string;
        handle.post(RefreshLable);
    }

    private static Runnable RefreshLable=new Runnable(){
        @Override
        public void run(){
            textview.setText(str1);
        }
    };

    @Override
    public void onCreate(Bundle onSaveInstanceState){
        super.onCreate(onSaveInstanceState);
        setContentView(R.layout.activity_main);

        final TextView textview = (TextView)findViewById(R.id.show);
        final Button resetButton=(Button)findViewById(R.id.reset);
        final Button beginButton=(Button)findViewById(R.id.start);
        final Button stopButton=(Button)findViewById(R.id.stop);
        final Intent serviceIntent=new Intent(this,produce.class);

        resetButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                textview.setText(00+":"+00+":"+000);
            }
        });
        beginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(serviceIntent);
            }
        });
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(serviceIntent);
            }
        });
    }
}


produce.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import java.util.Calendar;

public class produce extends Service{

    private Thread thread;
    private int mi0,s0,ms0;

    @Override
    public void onCreate(){
        super.onCreate();
        thread=new Thread(null,backgroundThread,"thread");
    }

    @Override
    public void onStart(Intent intent,int startId){
        super.onStart(intent,startId);
        if(!thread.isAlive()){
            thread.start();
            Calendar cal=Calendar.getInstance();
            mi0=cal.get(Calendar.MINUTE);
            s0=cal.get(Calendar.SECOND);
            ms0=cal.get(Calendar.MILLISECOND);
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        thread.interrupt();
    }

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    private Runnable backgroundThread=new Runnable() {
        @Override
        public void run() {
            try{
                while(!Thread.interrupted()){
                    Calendar cal=Calendar.getInstance();
                    int mi=Math.abs(cal.get(Calendar.MINUTE)-mi0);
                    int s=Math.abs(cal.get(Calendar.SECOND)-s0);
                    int ms=Math.abs(cal.get(Calendar.MILLISECOND)-ms0);
                    String str=mi+":"+s+":"+ms;
                    MainActivity.UpdateGUI(str);
                    Thread.sleep(0);
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    };
}

androidmanifest.xml



    
        
            
                

                
            
        
        
    



你可能感兴趣的:(Android实验5:使用线程)