博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Handler例子
阅读量:3587 次
发布时间:2019-05-20

本文共 2313 字,大约阅读时间需要 7 分钟。

简单例子

package com.peidw;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Message;import android.text.Html;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;/** *  * @author peidw * */public class HandlerThreadActivity extends Activity {	private TextView mt; 	private Handler mcHandler=new Handler(){ 		@Override 		public void handleMessage(Message msg) { 			 mt.setText(Html.fromHtml((String)msg.obj)); 			mcHandler.post( update_thread);  		} 	}; 		private boolean isUpdateInfo=true;		//与UI线程管理的handler     private static final int MSG_UPDATE_INFO = 0x110;    	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_handler_thread);		//创建后台线程 		 		mt = (TextView) findViewById(R.id.id_textview);		 		mcHandler.post(update_thread);	}	  	@Override	public boolean onCreateOptionsMenu(Menu menu) {		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.handler_thread, menu);		return true;	}	@Override	public boolean onOptionsItemSelected(MenuItem item) {		// Handle action bar item clicks here. The action bar will		// automatically handle clicks on the Home/Up button, so long		// as you specify a parent activity in AndroidManifest.xml.		int id = item.getItemId();		if (id == R.id.action_settings) {			return true;		}		return super.onOptionsItemSelected(item);	}		private void initBack(){		Log.i ("AA----->","-----------------initBack");		//ht=new   HandlerThread("check-message-coming");		//ht.start();		Log.i ("AA----->","-----------------isUpdateInfo="+isUpdateInfo);			}	 Runnable update_thread = new Runnable()	 {		 int i=0;		 public void run(){			 Message msg = mcHandler.obtainMessage();			 			 try {				Thread.sleep(2000);			} catch (InterruptedException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}             String result = "实时更新中,当前大盘指数:%d";             result = String.format(result, (int) (Math.random() * 3000 + 1000));             msg.obj=result;             mcHandler.sendMessage(msg);             if(i>100){            	 mcHandler.removeCallbacks(update_thread);             }		 }	 };	  	 		}

layout 

转载地址:http://ylvwn.baihongyu.com/

你可能感兴趣的文章
django2.0,python3.7连接sql_server
查看>>
Python 生成requirement及使用requirements.txt安装类库
查看>>
multiprocessing.pool多线程的使用
查看>>
非计算机专业本科毕业如何迅速成长为一名算法工程师
查看>>
关于自然语言处理(NLP)的个人学习资料
查看>>
BERT
查看>>
Java keytool生成jks证书,并使用openssl查看公钥信息
查看>>
mysql创建存储过程,set动态赋值
查看>>
【c语言】蓝桥杯算法提高 Quadratic Equation
查看>>
【c语言】蓝桥杯算法提高 输入输出格式练习
查看>>
【c语言】蓝桥杯算法提高 勾股数
查看>>
【c语言】蓝桥杯算法提高 c++_ch02_04
查看>>
【c语言】蓝桥杯算法提高 3-1课后习题2
查看>>
【c语言】蓝桥杯算法提高 3-2求存款
查看>>
【c语言】蓝桥杯算法提高 3-3求圆面积表面积体积
查看>>
【c语言】蓝桥杯算法提高 P0401
查看>>
【c语言】蓝桥杯算法提高 P0402
查看>>
【c语言】蓝桥杯算法提高 三个整数的排序
查看>>
【c语言】蓝桥杯算法提高 P0101
查看>>
【c语言】统计字符次数
查看>>