线程问题(第五个狗东西)
MainActivity.java
package com.example.administrator.exp5;
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;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
private static Handler handler = new Handler();
private Button btnResume;
private Button btnStart;
private Button btnPause;
private static TextView textView2;
private static TextView textView4;
private static TextView textView6;
public static int hour;
public static int min;
public static int sec;
public static void UpadteGUI(int h, int m, int s) {
hour = h;
min = m;
sec = s;
handler.post(RefreshLable);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnResume = (Button)findViewById(R.id.btnResume);
btnStart = (Button)findViewById(R.id.btnStart);
btnPause =(Button) findViewById(R.id.btnPause);
textView2= (TextView)findViewById(R.id.textView2);
textView4= (TextView)findViewById(R.id.textView4);
textView6 = (TextView)findViewById(R.id.textView6);
final Intent intent = new Intent(this, RandomService.class);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(intent);
}
});
btnResume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("00");
textView4.setText("00");
textView6.setText("00");
stopService(intent);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
}
});
}
private static Runnable RefreshLable = new Runnable() {
@Override
public void run() {
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(2);
textView2.setText(nf.format(hour));
textView4.setText(nf.format(min));
textView6.setText(nf.format(sec));
}
};
}
RandomService.java
package com.example.administrator.exp5;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class RandomService 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();
}
}
@Override
public void onDestroy() {
//super.onDestroy();
workthread.interrupt();
}
private Runnable backgroudWork = new Runnable() {
@Override
public void run() {
LayoutInflater layoutInflater = LayoutInflater.from(RandomService.this);
View view = layoutInflater.inflate(R.layout.activity_main,null);
TextView textView2 = view.findViewById(R.id.textView2);
TextView textView4= view.findViewById(R.id.textView4);
TextView textView6 = view.findViewById(R.id.textView6);
int h = Integer.parseInt(textView2 .getText().toString());
int m = Integer.parseInt(textView4 .getText().toString());
int s = Integer.parseInt(textView6.getText().toString());
while (true) {
try {
Thread.sleep(1000);
s++;
if (s >= 60) {
m++;
s = 0;
if (m >= 60) {
h++;
m = 0;
if (h >= 24) {
h = 0;
}
}
}
MainActivity.UpadteGUI(h,m,s);
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
};
}
Main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="秒表"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" 00"
android:textSize="40sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" : "
android:textSize="40sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" 00 "
android:textSize="40sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" : "
android:textSize="40sp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="00 "
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:gravity="center">
<Button
android:id="@+id/btnResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 清零" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 计时" />
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 暂停" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.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=".RandomService" >
</service>
</application>
</manifest>