需求:想使用異步 把數據傳值過去,在傳回來 。
1 使用接口回調
首先 定義一個接口,
public interface My_interface { void setsrc(String src); }2 寫一個 異步加載任務
// 命名不太規範 -。- 不要介意哈!
// 想要把數據傳遞 就要對外提供一個 方法
public class text2 extends AsyncTask<String ,Intent ,String> { private My_interface in; // 對外提供的方法 public text2(My_interface in) { this.in = in; } @Override protected String doInBackground(String... params) { if (params[0]!=null){ in.setsrc(params[0]); } return null; } } //實現對外提供的方法 ,並且給他數據 ,然後回傳回來
new text2(new My_interface() { @Override public void setsrc(String src) { // 得到數據並設置 src 回傳來的數據 text2.setText(src); } }).execute("給我的 AsyncTask");