关于Timer、ListView和Handler
作者:互联网
1、关于Timer。UHF中为实现每隔几秒读一次标签,采用Timer的方式。由于读完后需要暂停,直接timer或者task进行cancel后,程序下次再想使用时就会报错。具体解决办法为将initTimer以及销毁Timer独立出来,每次cancel后赋值新的timer以及task。
参考网址:
https://blog.csdn.net/muyangbin/article/details/47833199
https://github.com/sfyc23/CountDownList/blob/master/app/src/main/java/com/sfyc/countdownlist/TimerSimpleActivity.java
https://www.jianshu.com/p/b241b6939b2b
2、Handler的三种方法
//方法1
Message msg = new Message();
msg.what = 1;
msg.arg1 = 2;
msg.arg2 = 3;
msg.obj = "demo";
mHandler.sendMessage(msg);
//方法2
Message msg2 = mHandler.obtainMessage();
//obtainMessage();
//obtainMessage(what);
//obtainMessage(int what,Object obj);
//obtainMessage(int what,int arg1,int arg2);
//obtainMessage(int what,int arg1,int arg2,Object obj );
msg2.what = 1;
msg2.arg1 = 2;
msg2.arg2 = 3;
msg2.obj = "demo";
msg2.sendToTarget();
//方法3
Message msg3 = Message.obtain();
msg3.sendToTarget();
推荐使用二和三,参考网址:
https://blog.csdn.net/u011791526/article/details/53540346
3、使用listview进行添加时的处理
1)add(hamp)与set(sn,hamp)的区别,一个添加,一个修改
2)hamp.put中两个参数为一个key一个value
3)Adapter.notifyDataSetChanged()源码分析以及与ListView.setAdapter的区别
参考网址:https://www.cnblogs.com/nailperry/p/4668553.html
PS:使用java时经常与C混淆,String与int的转换,不一样
int
a = Integer.parseInt(str);
标签:what,msg2,int,Timer,Handler,obtainMessage,msg,Message,ListView 来源: https://www.cnblogs.com/wack/p/10461106.html