您的位置:首页 > 移动开发 > Android开发

Android:ANR、线程间通讯、Handler、Message

2013-05-21 19:54 471 查看
场景:点击按钮1进行下载,显示进度条进度变化,10秒后下载完成,进度条消失;点击按钮2完成更新,出现转动进度条,5秒后更新完毕进度条消失。
public class MainActivity extends Activity implements OnClickListener
{
TextView textView = null;
private ProgressBar mProgress_yuan;
private ProgressBar mProgress_jindu;
private final int MSG_WHAT_DOWNLOAD = 1;
private final int MSG_WHAT_UPDATE = 2;
private final int MSG_WHAT_PROGRESS = 3;

private Handler mHandler = new Handler()// Handler运行于main线程,在main线程接收消息
{
@Override
public void handleMessage(Message msg)// 重写handleMessage方法
{
switch (msg.what)
{
case MSG_WHAT_DOWNLOAD:
Data data = (Data) msg.obj;
textView.setText(data.toString());
mProgress_jindu.setVisibility(View.GONE);// 下载完成后,进度条消失
break;

case MSG_WHAT_UPDATE:
String str = (String) msg.obj;
textView.setText(str);
mProgress_yuan.setVisibility(View.GONE);// 更新完毕后,圆形进度框消失
break;
case MSG_WHAT_PROGRESS:
int arg = msg.arg1;
mProgress_jindu.setProgress(arg);// 每次接收消息,更新进度条
break;

default:
break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView1);
mProgress_yuan = (ProgressBar) findViewById(R.id.progressBar1);
mProgress_jindu = (ProgressBar) findViewById(R.id.progressBar2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
btn1Click();
break;
case R.id.button2:
btn2Click();
break;

default:
break;
}
}

private void btn1Click()// 点击下载,10秒后下载完成,进度条消失
{
mProgress_jindu.setVisibility(View.VISIBLE);// 点击时进度条显示(xml设置默认隐藏)
new Thread()
{
@Override
public void run()
{
Message message = new Message();
message.what = MSG_WHAT_DOWNLOAD; // 设置标识
message.obj = download();// 将返回结果赋给msg,调用下载方法时同时发消息更新进度条
mHandler.sendMessage(message);// 通过Handler传递msg
}
}.start();

}

private void btn2Click()// 点击更新,圆形进度条转动,3秒后更新完成,进度条消失
{
mProgress_yuan.setVisibility(View.VISIBLE);
new Thread()
{
@Override
public void run()
{
try
{
Thread.sleep(3000);
String str = update();

//Message msg = mHandler.obtainMessage(what, obj)
//下面部分可以采用上面的方法优化
Message msg = new Message();
msg.what = MSG_WHAT_UPDATE;
msg.obj = str;
mHandler.sendMessage(msg);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}.start();
}

private Data download()
{
for (int i = 0; i < 10; i++)// 模拟下载进度过程
{
//Message msg = new Message();
Message msg = mHandler.obtainMessage();//从系统的资源池获取复用msg,避免重复创建msg对象
msg.what = MSG_WHAT_PROGRESS;
msg.arg1 = i * 10;
mHandler.sendMessage(msg);// 每次进度发消息给main线程更新进度条
try
{
Thread.sleep(1000);// 10秒后下载完成
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
Data data = new Data("下载完成!", 12);
return data;
}

private String update()
{
return "更新完成!";
}

class Data
{
String name;
int num;

public Data(String name, int num)
{
super();
this.name = name;
this.num = num;
}

@Override
public String toString()
{
return "data [name=" + name + ", num=" + num + "]";
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐