哈哈,博文取了個比較霸氣的名字,你們不都喜歡這樣忽悠人嗎 呵呵!java
好了,如今就是很簡單的點擊查詢,而後這個查詢有點花時間,不想見面出現假死現象,因此在另外的線程進行查詢。android
好了,代碼在此:app
package com.example.gulanfinddemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.widget.EditText; public class FindResActivity extends Activity { private MyHandler handler; private Boolean isKeywordFindBoolean = false; EditText resText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_res); resText = (EditText)findViewById(R.id.ResText); Log.v("FindResActivity threadId:", String.valueOf(Thread.currentThread().getId())); Intent intent = getIntent(); String findReString; if (null != intent.getStringExtra(MainActivity.suraAyaFindStr)) { findReString = "按章節查詢"; isKeywordFindBoolean = false; } else { findReString = "按關鍵字查詢"; isKeywordFindBoolean = true; } Looper looper = Looper.myLooper(); handler = new MyHandler(looper); new findContentThread(handler, findReString).start(); //resText.setText(findReString); } private class MyHandler extends Handler{ public MyHandler(Looper looper){ super(looper); } public void handleMessage(Message msg){ if (msg.what == 0) { Bundle bundle = msg.getData(); resText.setText(bundle.getString("res")); } } } }
看出來了,重載了handler,在ide
handleMessage裏會進行UI的更新。
package com.example.gulanfinddemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class findContentThread extends Thread{
private String queryKeyString;
private Handler handler;
public findContentThread(Handler sendHandler,String string){
queryKeyString = string;
handler = sendHandler;
}
public void run(){
Log.v("findContentThread threadId:", String.valueOf(Thread.currentThread().getId()));
Message msgMessage = new Message();
Bundle bundle = new Bundle();
bundle.putString("res", queryKeyString);
msgMessage.setData(bundle);
msgMessage.what = 0;
handler.sendMessage(msgMessage);
super.run();
}
}
在這裏用了擴展Thread的方法,處理以後handler會傳回消息,以後就在handler裏處理消息了。oop
而後裏面打印了線程ID,從其中能夠肯定的確是在不一樣的線程裏執行的。spa