标签:
异步跟新UI:
1、handler+Thread(runnable):如果handler和Thread都写在了一个Java文件中,就不说了,如果runnable定义在了一个单独的类文件中,可以通过在构造方法里接收handler参数,然后执行完耗时操作后,通过handler发送消息来通知主UI线程更新UI
2、接口回调,定义一个接口,然后在主UI执行耗时操作的时候,借助匿名内部类,在这里写异步线程返回来的数据处理操作,异步线程接收一个匿名的内部类实例,然后在执行完耗时操作后回调接口的方法,可以把耗时的操作的结果返回给回调函数
1的代码实现:
package com.example.testtesttest; import org.json.JSONArray; import com.example.testtesttest.DataRunnable.dataCallBackLisener; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements android.view.View.OnClickListener{ private Button button = null; private dataCallBackLisener callbackListener ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: DataRunnable runnable = new DataRunnable(handler); Thread thread = new Thread(runnable); thread.start(); break; default: break; } } public Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: button.setText("hhh"); break; default: break; } }; }; }
package com.example.testtesttest; import android.os.Handler; public class DataRunnable implements Runnable{ private String[] dataString = new String[5]; private dataCallBackLisener listener; private Handler dataHandler; public DataRunnable(dataCallBackLisener alistener){ this.listener =alistener; } public DataRunnable(Handler aHandler){ this.dataHandler =aHandler; } @Override public void run() { // TODO Auto-generated method stub for(int i =0 ; i <5;i++) { dataString[i] = i+""; } dataHandler.sendEmptyMessage(1); //listener.dataCallback(dataString); } public interface dataCallBackLisener{ public void dataCallback(String[] str); } }
2的代码实现:
package com.example.testtesttest; import org.json.JSONArray; import com.example.testtesttest.DataRunnable.dataCallBackLisener; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements android.view.View.OnClickListener{ private Button button = null; private dataCallBackLisener callbackListener ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* JSONArray json = new JSONArray(); for(int i =0 ; i<4 ; i++){ json.put(i); } String[] a = {"1","2","3"}; Log.e("Loooooooooook",a.toString());*/ button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: DataRunnable runnable = new DataRunnable(new dataCallBackLisener() { @Override public void dataCallback(String str) { // TODO Auto-generated method stub Log.e("hahahaahahahah", str.toString()); button.setText(str.toString()); //更新UI } }); runOnUiThread(runnable); /*DataRunnable runnable = new DataRunnable(handler); Thread thread = new Thread(runnable); thread.start();*/ /*AnotherFragment fragment = new AnotherFragment(); android.app.FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit();*/ break; default: break; } } public Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: button.setText("hhh"); break; default: break; } }; }; }
package com.example.testtesttest; import android.os.Handler; public class DataRunnable implements Runnable{ private StringBuilder dataString = new StringBuilder(); private dataCallBackLisener listener; private Handler dataHandler; public DataRunnable(dataCallBackLisener alistener){ this.listener =alistener; } public DataRunnable(Handler aHandler){ this.dataHandler =aHandler; } @Override public void run() { // TODO Auto-generated method stub for(int i =0 ; i <5;i++) { dataString.append(i); } //dataHandler.sendEmptyMessage(1); listener.dataCallback(dataString.toString()); } public interface dataCallBackLisener{ public void dataCallback(String str); } }
注意2中,thread必须是UIthread,这样才能更新主UI的控件
最后对回调函数的理解博客:
http://www.zhihu.com/question/19801131
http://www.jianshu.com/p/cd35ce823602
http://www.bkjia.com/Androidjc/853632.html
最后一篇讲了名词:接口对象???不懂。。。。
标签:
原文地址:http://www.cnblogs.com/lucky-star-star/p/4454171.html