码迷,mamicode.com
首页 > 移动开发 > 详细

Android 线程模型

时间:2016-03-18 20:22:21      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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 线程模型

标签:

原文地址:http://www.cnblogs.com/sytfyf/p/5293301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!