Android 线程模型
Posted SYT在路上
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 线程模型相关的知识,希望对你有一定的参考价值。
android 线程模型
1.
import android.os.Handler;
import android.os.Message;
public class MainActivity extends Activity
{
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what)
{
case 1:
//Code Run In UI Thread
break;
default:
break;
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
Thread thread=new Thread(new Runnable()
{
@Override
public void run()
{
//Code Run In Thread
Message message=new Message();
message.what=1;
mHandler.sendMessage(message);
}
});
thread.start();
}
}
2.
Activity currentActivity = ContextUtils.getCurrentActivity();
final Handler handler = new Handler(currentActivity.getMainLooper());
(new Thread(new Runnable() {
public void run() {
//Code Run In Thread
handler.post(new Runnable() {
public void run() {
//Code Run In UI Thread
}
});
}
})).start();
以上是关于Android 线程模型的主要内容,如果未能解决你的问题,请参考以下文章