android 中在进行耗时操作时通常需要开启新的Thread,而在结束后可以通过handler发送消息进行主线程的更新。在新的Thread中如果需要进行大量逻辑处理就会书写大量的代码。所以可以在Thread中使用回调机制。
定义包含回调函数的类
1.自定义接口
2.实现接口对象
3.为对象赋值 (在需要的调用的地方new接口对象实现接口)
4.回调接口 把数据传回需要的地方
public class Budds {
private ResultCallBack resultCallBack; //实现接口对象(2)
public Budds(int a ,int b ,ResultCallBack resultCallBack) {
super();
this.resultCallBack = resultCallBack;//在构造含函数中 对接口对象赋值(3)
initView(a,b);
}
private void initView(int a,int b){
try {
Thread.sleep(3000);//休眠3秒
int c = a+b ;//进行加法运算
resultCallBack.JSONResult("计算结果"+c);//回调接口 传回数据(4)
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface ResultCallBack { //自定义的接口(1)
void JSONResult(String s);
}
}
在需要的地方调用接口回调
1.new 一个包含接口对象的类
2.继承接口实现方法
public void but3(View view) {
ToastUtil.showToast(Ten_six_one.this,"开始计算请耐心等待");
new Thread(new Runnable() {
@Override
public void run() {
Budds budds = new Budds(3,4,new Budds.ResultCallBack() {//new 一个包含接口对象的类(1)
@Override
public void JSONResult(String s) {//继承接口实现方法(2)
Message message = new Message();
message.what = 1;
Bundle bundle = new Bundle();
bundle.putString("string",s);
message.setData(bundle);
handler.sendMessage(message);//发送handler(3)
}
});
}
}).start();
handler
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1 :
String string_name =msg.getData().getString("string");
ToastUtil.showToast(Ten_six_one.this,string_name);
break;
case 0x11:
break;
}
}
};
其余代码
package com.taisheng.fh.myapplication.ten_six;
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 com.lpf.lpf.utils.ToastUtil;
import com.taisheng.fh.myapplication.R;
public class Ten_six_one extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ten_six_one);
}
// public void but1(View view) {
//
// ToastUtil.showToast(Ten_six_one.this,"ten_one_one");
//
// Intent intent = new Intent();
// intent.setClass(Ten_six_one.this,Ten_six_two.class);
// startActivity(intent);
// }
//
// public void but2(View view) {
// ToastUtil.showToast(Ten_six_one.this,"ten_one_two");
// Intent intent = new Intent();
// intent.setClass(Ten_six_one.this,Ten_six_three.class);
// startActivity(intent);
// }
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1 :
String string_name =msg.getData().getString("string");
ToastUtil.showToast(Ten_six_one.this,string_name);
break;
case 0x11:
break;
}
}
};
public void but3(View view) {
ToastUtil.showToast(Ten_six_one.this,"开始计算请耐心等待");
new Thread(new Runnable() {
@Override
public void run() {
Budds budds = new Budds(3,4,new Budds.ResultCallBack() {
@Override
public void JSONResult(String s) {
Message message = new Message();
message.what = 1;
Bundle bundle = new Bundle();
bundle.putString("string",s);
message.setData(bundle);
handler.sendMessage(message);
}
});
}
}).start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_centerInParent="true"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="but1"
android:visibility="gone"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="but2"
android:visibility="gone"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="but3"/>
</LinearLayout>
package com.taisheng.fh.myapplication.ten_six;
/**
* Created by Administrator on 2016/11/19.
*/
public class Budds {
private ResultCallBack resultCallBack;
public Budds(int a ,int b ,ResultCallBack resultCallBack) {
super();
this.resultCallBack = resultCallBack;
initView(a,b);
}
private void initView(int a,int b){
try {
Thread.sleep(3000);
int c = a+b ;
resultCallBack.JSONResult("计算结果"+c);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public interface ResultCallBack {
void JSONResult(String s);
}
}