1.第一种方法

package com.example.jer824;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView tv;

private TextView tv1;

int a=100;

private Handler handler=new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what==1){

Log.d("??????",a+"");

tv.setText(a+"");

a--;

Log.d("??????","成功");

new GetCache(a).run();

}else{tv.setText("倒计时结束");}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv=(TextView)findViewById(R.id.tv);

tv1=(TextView)findViewById(R.id.tv1);

tv1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

new GetCache(a).run();

}

});

}

private class GetCache implements Runnable { //两种方法继承或接口

private int id;

public GetCache(int id) {

this.id = id;

}

@Override

public void run() {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

Message message = new Message();

if (id==0) {

message.what = 2;

handler.sendMessage(message);

}else {

message.what = 1;

handler.sendMessage(message);

}

}

}

}

2.第二种方法

package com.example.jer824;

import android.os.Bundle;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

private TextView tv;

private Button bt;

//线程间通信

private android.os.Handler handler=new android.os.Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

tv.setText(msg.what+"");

Bundle bundle=msg.getData();

String str=bundle.getString("a");

tv.setText(str+msg.what);

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

tv= (TextView) findViewById(R.id.tv);

bt= (Button) findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

new Thread(new GetCache()).start();

// new Thread(new Runnable() {

// @Override

// public void run() {

// int a=100;

// while (a>=0){

// try {

// Thread.sleep(100);

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// a--;

// Message message=new Message();

// message.what = a;

// handler.sendMessage(message);

// }

// }

// }).start();

}

});

}

//多线程 子线程

//public class GetCache extends Thread

public class GetCache implements Runnable{

@Override

public void run() {

//子线程运行完了,去主线程

int a=100;

while (a>=0){

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

a--;

Message message=new Message();

message.what = a;

Bundle bundle=new Bundle();

bundle.putString("a","AAA");

message.setData(bundle);

// handler.sendMessageDelayed(message,200);

handler.sendMessage(message);

//与上面三行一个意思

// handler.sendEmptyMessage(a);

}

}

}

}

3.第三种方法

package com.example.jer824;

import android.os.Bundle;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {

private TextView tv;

private Button bt;

private int count=100;

//线程间通信

private android.os.Handler handler=new android.os.Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

//tv.setText(msg.what);

if(count==0){

return;

}

tv.setText(msg.what + "");

handler.sendEmptyMessageDelayed(count--,500);

//tv.setText(msg.what + "");

//bundle传值

// Bundle bundle=msg.getData();

// String str = bundle.getString("a");

// tv.setText(str+msg.what);

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

tv= (TextView) findViewById(R.id.tv);

bt= (Button) findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

handler.sendEmptyMessage(count);

//

// new Thread(new Runnable() {

// @Override

// public void run() {

// int a=100;

// while (a>0){

// try {

// Thread.sleep(100);

// } catch (InterruptedException e) {

// e.printStackTrace();

// }

// a--;

// // tv.setText(a+"");

// Message message=new Message();

// message.what = a;

// handler.sendMessage(message);

// }

// }

// }).start();

//new Thread(new GetCache()).start();

}

});

}

//多线程 子线程

//public class GetCache extends Thread

public class GetCache implements Runnable{

@Override

public void run() {

//子线程运行完了,去主线程

int a=100;

while (a>0){

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

a--;

// tv.setText(a+"");

Message message=new Message();

message.what = a;

//加入bundle值

// Bundle bundle=new Bundle();

// bundle.putString("a","1");

// message.setData(bundle);

handler.sendMessage(message);

//多久后启动

//handler.sendMessageAtTime(message,500);

//handler.sendMessageDelayed(message,500);

//handler.sendEmptyMessage(a);传空值

}

}

}

}

37bad4407dad61957dcbc4a4b7775d2c.png

a6417c6d390489d7564642acbeda807f.png

187120_0.jpg

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐