protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
handerHandler.removeMessages(0);//如果不在这里把消息从列队里删除,则在后台里继续发消息。
}
private TextView textView1;
public static int add;
private Handler handerHandler = new Handler() {
/*
* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
// textView1.setText("add");
textView1.setText(""+add+"");//不这么写,像上面那么写会出错,程序运行时强制关闭。
++add;
removeMessages(0);
sendEmptyMessageDelayed(0, 5000);
Log.e("I AM HANDLER", "I AM TEST>>>>>>>>>>>>>>>>>>>>>>>O");
break;
case 1:
break;
case 3:
break;
case 4:
break;
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
handerHandler.sendEmptyMessage(0);//这里发送了一个空的消息0到MessageQueue,也可以用下面2种的方法。
// Message msg = new Message();
// msg.what=0;
// handerHandler.sendMessage(msg);
//这用方法比上面那种Message的获取方法节省内部空间
// Message msg=handerHandler.obtainMessage();
// msg.arg1=0;
// handerHandler.sendMessage(msg);
}