项目中我们为了避免不阻碍UI线程,进程会创建另外的线程来执行操作,在android中有两种实现线程thread的方法:一种就是继承Thread类,一种是实现Runnable接口
使用线程我们就离不开Handler。
在一个线程中更新UI,我们都知道handler + thread 代码如下:
public class MainActivity extends Activity {
private EditmText mText;
private Button mButton;
private myHandler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (EditmText)findViewById(R.id.ui_txt);
mButton = (Button)findViewById(R.id.update_ui_btn);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
handler = new myHandler();
myThread thread = new myThread();
thread.start();
}
});
}
private class myHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
String str = bundle.getString("str");
mText.setmText(str);
}
}
private class myThread extends Thread{
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("str", "名字");
msg.setData(bundle);
MainActivity.this.myHandler.sendMessage(msg);
}
}
}
上面是继承Thread类来创建线程,我们也可以实现Runnable接口
public class MainActivity extends Activity {
private EditmText mText;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (EditmText)findViewById(R.id.ui_txt);
mButton = (Button)findViewById(R.id.update_ui_btn);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
thread.start();
}
});
}
public Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
String str = bundle.getString("str");
mText.setmText(str);
}
}
final Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putString("str", "名字");
msg.setData(bundle);
handler.sendMessage(msg);
}
}
}
有时候我们在项目中经常用到计时器。代码如下:
public class MainActivity extends ActionBarActivity {
private TextView mTime;
private Button mStart;
private int cunt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mTime = (TextView) findViewById(R.id.main_text);
mStart = (Button) findViewById(R.id.main_bt);
mStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//将线程添加到队列里
handler.post(updateThread);
}
});
}
//创建一个Handler对象
Handler handler = new Handler();
//将要执行的操作写在线程对象的run方法当中
Runnable updateThread = new Runnable(){
@Override
public void run() {
if(cunt < 100){
cunt++;
mTime.setText(cunt + "s");
//在run方法内部,执行postDelayed或者是post方法
handler.postDelayed(updateThread, 500);
}
}
};
}
还有runOnUiThread(Runnable)更新UI,我们都知道AsyncTask中的onPostExecute方法中可以更新UI,但是有时候我们要在doInBackground方法中更新UI,我们就可以使用runOnUiThread(Runnable)来更新UI
代码如下:
class saveVideoTask extends AsyncTask<String, Integer, Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params) {
//runOnUiThread(Runnable)(runOnUiThread是Activity中的方法,在线程中我们需要告诉系统是哪个activity调用,所以前面显 // 示的指明了activity。)
Activity.this. runOnUiThread(new Runnable() {
@Override
public void run() {
//更新UI操作
}
});
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
}
}