大家都知道Android遵循单线程模型,下面有个例子感到困惑。public class Activity01 extends Activity
{
private Button mButton01,mButton02;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
m_pDialog.setProgress(m_count++);
}
};
int m_count = 0;
//声明进度条对话框
ProgressDialog m_pDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("Activity:", Thread.currentThread().getId()+"");
Log.i("ActivityName:", Thread.currentThread().getName()+"");
//得到按钮对象
mButton01 = (Button)findViewById(R.id.Button01);
mButton02 = (Button)findViewById(R.id.Button02);
//设置mButton01的事件监听
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{ //创建ProgressDialog对象
m_pDialog = new ProgressDialog(Activity01.this); // 设置进度条风格,风格为圆形,旋转的
m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个圆形进度条对话框"); // 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img1); // 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 设置ProgressDialog 的一个Button
m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i)
{
//点击“确定按钮”取消对话框
dialog.cancel();
}
}); // 让ProgressDialog显示
m_pDialog.show();
}
});
//设置mButton02的事件监听
mButton02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
m_count = 0;
// 创建ProgressDialog对象
m_pDialog = new ProgressDialog(Activity01.this);
// 设置进度条风格,风格为长形
m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个长形对话框进度条");
// 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img2);
// 设置ProgressDialog 进度条进度
m_pDialog.setProgress(100);
// 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 让ProgressDialog显示
m_pDialog.show();
new Thread()
{
public void run()
{
try
{
while (m_count <= 100)
{
//第二种方式,单线程模型
// Message msg = new Message();
// handler.sendMessage(msg);
Log.i("handler:", Thread.currentThread().getId()+"");
Log.i("handlerName:", Thread.currentThread().getName()+"");
// 第一种方式,由线程来控制进度。
m_pDialog.setProgress(m_count++);
Thread.sleep(100);
}
m_pDialog.cancel();
}
catch (InterruptedException e)
{
m_pDialog.cancel();
}
}
}.start();
}
});
}
}上面的第二种方式,我理解,是遵循了单线程模型,让UI主线程来处理,为什么第一种方式也可以,不是子线程不可以处理
UI界面的空间吗?我也打印了 线程ID 是不一样的。求解答???
{
private Button mButton01,mButton02;
private Handler handler = new Handler(){
public void handleMessage(Message msg){
m_pDialog.setProgress(m_count++);
}
};
int m_count = 0;
//声明进度条对话框
ProgressDialog m_pDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("Activity:", Thread.currentThread().getId()+"");
Log.i("ActivityName:", Thread.currentThread().getName()+"");
//得到按钮对象
mButton01 = (Button)findViewById(R.id.Button01);
mButton02 = (Button)findViewById(R.id.Button02);
//设置mButton01的事件监听
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{ //创建ProgressDialog对象
m_pDialog = new ProgressDialog(Activity01.this); // 设置进度条风格,风格为圆形,旋转的
m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个圆形进度条对话框"); // 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img1); // 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 设置ProgressDialog 的一个Button
m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i)
{
//点击“确定按钮”取消对话框
dialog.cancel();
}
}); // 让ProgressDialog显示
m_pDialog.show();
}
});
//设置mButton02的事件监听
mButton02.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
m_count = 0;
// 创建ProgressDialog对象
m_pDialog = new ProgressDialog(Activity01.this);
// 设置进度条风格,风格为长形
m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog 标题
m_pDialog.setTitle("提示");
// 设置ProgressDialog 提示信息
m_pDialog.setMessage("这是一个长形对话框进度条");
// 设置ProgressDialog 标题图标
m_pDialog.setIcon(R.drawable.img2);
// 设置ProgressDialog 进度条进度
m_pDialog.setProgress(100);
// 设置ProgressDialog 的进度条是否不明确
m_pDialog.setIndeterminate(false);
// 设置ProgressDialog 是否可以按退回按键取消
m_pDialog.setCancelable(true);
// 让ProgressDialog显示
m_pDialog.show();
new Thread()
{
public void run()
{
try
{
while (m_count <= 100)
{
//第二种方式,单线程模型
// Message msg = new Message();
// handler.sendMessage(msg);
Log.i("handler:", Thread.currentThread().getId()+"");
Log.i("handlerName:", Thread.currentThread().getName()+"");
// 第一种方式,由线程来控制进度。
m_pDialog.setProgress(m_count++);
Thread.sleep(100);
}
m_pDialog.cancel();
}
catch (InterruptedException e)
{
m_pDialog.cancel();
}
}
}.start();
}
});
}
}上面的第二种方式,我理解,是遵循了单线程模型,让UI主线程来处理,为什么第一种方式也可以,不是子线程不可以处理
UI界面的空间吗?我也打印了 线程ID 是不一样的。求解答???
解决方案 »
- 无从下手啊。我是新手。 你们刚入门的时候,都做点什么应用呀?
- 怪了,为什么老提示我的找不到:@string/hello,这个是系统生成的呢?
- 模拟器能模拟蓝牙功能吗?
- RelativeLayout 中如何实现 Linearlayout 中 android:layout_weight 属性的效果?
- 绑定服务之后,可以得到服务对象,然后可以对服务进行什么操作呢?可否传送数据过去?
- 请教
- ophone sdk 1.0安装问题
- 在android中如何删除一个已经创建的数据库???
- 通过jni移植pjsip协议到android上出现error: undefined reference to 'srand'
- 定位出经纬度用自带的locationManager还是用第三方的SDK
- Android 在一个Dialog里面,如何FindViewById找到自定义的CheckBox
- 求UI组件名称
if (mUiThreadId == Thread.currentThread().getId()) {
doRefreshProgress(id, progress, fromUser);
} else {
RefreshProgressRunnable r;
if (mRefreshProgressRunnable != null) {
// Use cached RefreshProgressRunnable if available
r = mRefreshProgressRunnable;
// Uncache it
mRefreshProgressRunnable = null;
r.setup(id, progress, fromUser);
} else {
// Make a new one
r = new RefreshProgressRunnable(id, progress, fromUser);
}
post(r);
}
}看这个帖子
http://topic.csdn.net/u/20110720/09/2f82e197-f889-4195-8567-8952af35d979.html
543
@android.view.RemotableViewMethod
544
public synchronized void setProgress(int progress) {
545
setProgress(progress, false);
546
} synchronized void setProgress(int progress, boolean fromUser) {
550
if (mIndeterminate) {
551
return;
552
}
553
554
if (progress < 0) {
555
progress = 0;
556
}
557
558
if (progress > mMax) {
559
progress = mMax;
560
}
561
562
if (progress != mProgress) {
563
mProgress = progress;
564
refreshProgress(R.id.progress, mProgress, fromUser);
565
}
566
}
找了几个view,貌似只有ProgressDialog 用到了这个,它内部搞了个线程控制的。谢谢2#的。