//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.walker.exp5"> <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"> </service> </application> </manifest>
//MainActivity.java
package com.walker.exp5; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { private static Handler handler = new Handler(); private Button btn_clear; private Button btn_start; private Button btn_stop; private static TextView tv_hh; private static TextView tv_mm; private static TextView tv_ss; public static int hh; public static int mm; public static int ss; public static void UpadteGUI(int h, int m, int s) { hh = h; mm = m; ss = s; handler.post(RefreshLable); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_clear = findViewById(R.id.btn_clear); btn_start = findViewById(R.id.btn_start); btn_stop = findViewById(R.id.btn_stop); tv_hh = findViewById(R.id.tv_hh); tv_mm = findViewById(R.id.tv_mm); tv_ss = findViewById(R.id.tv_ss); final Intent intent = new Intent(this, MainService.class); //start btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // String s = tv_ss.getText().toString(); // tv_ss.setText(s); //Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); startService(intent); Toast.makeText(MainActivity.this,"开始计时",Toast.LENGTH_SHORT).show(); } }); // /clear btn_clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv_hh.setText("00"); tv_mm.setText("00"); tv_ss.setText("00"); stopService(intent); Toast.makeText(MainActivity.this,"清零",Toast.LENGTH_SHORT).show(); } }); //stop btn_stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { stopService(intent); Toast.makeText(MainActivity.this,"暂停计时",Toast.LENGTH_SHORT).show(); } }); } private static Runnable RefreshLable = new Runnable() { @Override public void run() { NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumIntegerDigits(2); tv_hh.setText(nf.format(hh)); tv_mm.setText(nf.format(mm)); tv_ss.setText(nf.format(ss)); } }; // public class MyThread implements Runnable { // int h = Integer.parseInt(tv_hh.getText().toString()); // int m = Integer.parseInt(tv_mm.getText().toString()); // int s = Integer.parseInt(tv_ss.getText().toString()); // // @Override // public void run() { // while (true) { // try { // Thread.sleep(1000); // } catch (Exception e) { // e.printStackTrace(); // break; // } // s++; // if (s >= 60) { // m++; // s = 0; //秒钟等于60,分钟加1,秒钟置0 // if (m >= 60) { // h++; // m = 0; // if (h >= 24) { // h = 0; // } // } // } // tv_hh.setText(String.valueOf(h)); // tv_mm.setText(String.valueOf(m)); // tv_ss.setText(String.valueOf(s)); // // } // // } // } // public void clear(View v) { // tv_hh.setText("00"); // tv_mm.setText("00"); // tv_ss.setText("00"); // stopService(new Intent(MainActivity.this, MainService.class)); // Toast.makeText(this, "clear", Toast.LENGTH_SHORT).show(); // // } // // public void start(View v) { // //thread = new Thread(new MyThread()); // start thread // //thread.start(); // startService(new Intent(MainActivity.this, MainService.class)); // Toast.makeText(this, "start", Toast.LENGTH_SHORT).show(); // // } // // public void stop(View v) { // Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show(); // thread.interrupt(); // //thread.run(); // //stopService(new Intent(MainActivity.this, MainService.class)); // } }
//MainService.java
package com.walker.exp5; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainService extends Service { private Thread workthread; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); workthread = new Thread(null,backgroudWork,"workThread"); //Log.e("ATG","onCreate"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); if(!workthread.isAlive()){ workthread.start(); } // Toast.makeText(this,"onStart()",Toast.LENGTH_SHORT).show(); //Log.e("ATG","onStartCommand"); } // @Override // public int onStartCommand(Intent intent, int flags, int startId) { // if(!workthread.isAlive()){ // workthread.start(); // } // Log.e("ATG","onStartCommand"); // return super.onStartCommand(intent, flags, startId); // // } @Override public void onDestroy() { //super.onDestroy(); workthread.interrupt(); // Log.e("ATG","onDestroy"); //Toast.makeText(this,"onDestroy()",Toast.LENGTH_SHORT).show(); } private Runnable backgroudWork = new Runnable() { @Override public void run() { LayoutInflater layoutInflater = LayoutInflater.from(MainService.this); View view = layoutInflater.inflate(R.layout.activity_main,null); TextView tv_hh = view.findViewById(R.id.tv_hh); TextView tv_mm = view.findViewById(R.id.tv_mm); TextView tv_ss = view.findViewById(R.id.tv_ss); int h = Integer.parseInt(tv_hh.getText().toString()); int m = Integer.parseInt(tv_mm.getText().toString()); int s = Integer.parseInt(tv_ss.getText().toString()); while (true) { try { Thread.sleep(1000); s++; if (s >= 60) { m++; s = 0; //秒钟等于60,分钟加1,秒钟置0 if (m >= 60) { h++; m = 0; if (h >= 24) { h = 0; } } } MainActivity.UpadteGUI(h,m,s); } catch (Exception e) { e.printStackTrace(); break; } } } }; }
//activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv_miaobiao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:text="@string/title_name" android:textSize="30sp" /> <TextView android:id="@+id/tv_hh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_mm" android:layout_toLeftOf="@id/tv_1" android:text="@string/init_number" android:textSize="30sp" /> <TextView android:id="@+id/tv_mm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_miaobiao" android:layout_centerInParent="true" android:text="@string/init_number" android:textSize="30sp" /> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_mm" android:layout_toLeftOf="@id/tv_mm" android:text=":" android:textSize="30sp" /> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_mm" android:layout_toRightOf="@id/tv_mm" android:text=":" android:textSize="30sp" /> <TextView android:id="@+id/tv_ss" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/tv_mm" android:layout_toRightOf="@id/tv_2" android:text="@string/init_number" android:textSize="30sp" /> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清零" android:textSize="30sp" android:layout_alignTop="@id/btn_start" android:layout_toLeftOf="@id/btn_start" /> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计时" android:textSize="30sp" android:layout_below="@id/tv_mm" android:layout_centerInParent="true" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暂停" android:textSize="30sp" android:layout_alignTop="@id/btn_start" android:layout_toRightOf="@id/btn_start" /> </RelativeLayout>