一、android几种定时器的机制及区别。
答:在android中,经常用到的定时器主要有以下几种实现:
1、采用handler与线程的sleep(long)方法
开启一个线程,,每隔1s向Handler发送消息
public class MainActivity extends Activity { private Handler myhandler; private static int timemessage=0x01; private int timer=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myhandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if(msg.what==timemessage){ System.out.println("timer="+(++timer)); } } }; new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while(true){ try { System.out.println("执行了"); Thread.sleep(2000); myhandler.sendEmptyMessage(timemessage); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
2、采用Handler自带的postDelayed(Runnable,long)方法
利用延迟几秒发送Message,嵌套postDelayed()实现定时器。
public class MainActivity extends Activity { private Handler myhandler; private static int timemessage = 0x01; private int timer = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myhandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (msg.what == timemessage) { System.out.println("timer=" + (++timer)); } } }; myhandler.postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub myhandler.sendEmptyMessage(timemessage); myhandler.postDelayed(this, 1000); if (timer == 10) { myhandler.removeMessages(timemessage); } } }, 1000); } }3、采用Handler与Timer、TimerTask结合的方法
利用Timer类的schedule()方法,每隔一段时间做什么事情实现定时器。
public class MainActivity extends Activity { private Handler myhandler; private static int timemessage = 0x01; private int timer = 0; private Timer timerobject; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myhandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub if (msg.what == timemessage) { System.out.println("timer=" + (++timer)); } } }; timerobject=new Timer(); timerobject.schedule(new TimerTask() { //1s后,在每隔1s做什么事情 @Override public void run() { // TODO Auto-generated method stub myhandler.sendEmptyMessage(timemessage); } }, 1000,1000); } }
二、利用Listview显示姓名和年龄,采用SimpleAdapter实现。
public class MainActivity extends Activity { private ListView mylistview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylistview = (ListView) findViewById(R.id.listviewid); List<Map<String, Object>> listdata = new ArrayList<Map<String, Object>>(); //每行显示是一个Map对象 for (int i = 0; i < 10; i++) { Map mapdata = new HashMap<String, Object>(); mapdata.put("name", "limi"); mapdata.put("age", "30"); listdata.add(mapdata); } //第二个参数是ListView行布局 //第三个参数为存放Map元素的key值 //第四个参数将key值对应的元素在布局哪里显示 SimpleAdapter myadpter = new SimpleAdapter(getApplicationContext(), listdata, R.layout.listviewdatalayout, new String[] { "name", "age" }, new int[] { R.id.name, R.id.age }); mylistview.setAdapter(myadpter); } }
三、Android单例模式应用及优点
单例模式确保某一个类只有一个实例。通过使用private的构造函数确保在应用中只产生一个实例。
由于单例模式在内存中只有一个实例,减少了内存开销。
单例模式可以避免对资源的多重占用,例如一个写文件时,由于只有一个实例存在内存中,避免了对同一个资源的同时写操作。
使用单例模式,解决的就是多线程的同时创建的问题。
public class Person{ private Person(){}; private static Person zongtong; public static Person getPersonStance(){ if(zongtong==null){ synchronized(Person.class){ if(zongtong==null){ zongtong=new Person(); } } } return zongtong; } }