消息机制、线程间的通信

说明:

发送延时消息:

    消息立马发送,但不会立即处理,经过设置的延时时长后才会处理

    在android中,只有在UIThread中才能直接更新界面

    在android中,长时间的工作(联网)都需要在workerThread中执行

    在分线程获得服务器数据后,需要立即到主线程去更新界面显示数据

 

不实用handler实现异步工作:

private ProgressBar loding_bar;
	private TextView textViewp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		loding_bar = (ProgressBar) findViewById(R.id.progressBar1);
		textViewp = (TextView) findViewById(R.id.textViewp);
		
	}
	
	
	/**
	 * 不实用handler实现异步工作
	 * @param v
	 */
	public void getSubmit1(View v){
		//1.主线程,显示提示视图(progressdialog/progressBar)
		loding_bar.setVisibility(View.VISIBLE);
		//2分线程,联网亲求,得到响应数据
		new Thread(){
			public void run() {
				String pathString = "";
				try {
					final String result = requestToString(pathString);
					//3.主线程,显示数据
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							textViewp.setText(result);
							loding_bar.setVisibility(View.INVISIBLE);
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			private String requestToString(String pathString) throws Exception {
				URL url = new URL(pathString);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setConnectTimeout(5000);
				connection.setReadTimeout(5000);
				connection.connect();
				InputStream is = connection.getInputStream();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = -1;
				while((len = is.read(buffer))!=-1){
					baos.write(buffer,0,len);
				}
				baos.close();
				is.close();
				String result = baos.toString();
				connection.disconnect();
				return result;
			};
		}.start();
	}

 

使用handler实现异步工作:

 

使用步骤:

  1. 创建Handler成员变量对象,并重新、写其handlerMessage();

  2. 在分/主线程创建Message对象

  3. 使用handler对象发送Message

  4. 在handleMessage()中处理消息

 

public class MessageActivity extends Activity implements OnClickListener{

	private ProgressBar loding_bar;
	private TextView textViewp;
	
	
	//1.创建handler成员变量对象,重写其handlerMessage()
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==1){
				//4.在handlerMessage(0中处理消息
				String resultString = (String) msg.obj;
				textViewp.setText(resultString);
				loding_bar.setVisibility(View.INVISIBLE);
			}
		};
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		loding_bar = (ProgressBar) findViewById(R.id.progressBar1);
		textViewp = (TextView) findViewById(R.id.textViewp);
		
	}
	
	
	/**
	 * 使用handler实现异步工作
	 * 
	 */
	public void getSubmit2(View v){
		//1.主线程,显示提示视图
		loding_bar.setVisibility(View.VISIBLE);
		//2.分线程,联网请求,得到响应数据
		new Thread(){
			public void run() {
				String path = "";
				try {
					String result = requestToString(path);
					
					//3.主线程显示数据
						//3-1分线程创建Message对象
					Message message = Message.obtain();
					message.what =1;//标识
					message.obj = result;
					//3-2使用handler对象发送Message
					handler.sendMessage(message);
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			};
		}.start();
			
	}

	public  String requestToString(String pathString) throws Exception {
		URL url = new URL(pathString);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setConnectTimeout(5000);
		connection.setReadTimeout(5000);
		connection.connect();
		InputStream is = connection.getInputStream();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = -1;
		while((len = is.read(buffer))!=-1){
			baos.write(buffer,0,len);
		}
		baos.close();
		is.close();
		String result = baos.toString();
		connection.disconnect();
		return result;
	}
	
}

 

转载于:https://my.oschina.net/yabushan/blog/649560

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值