Thunder Android Face Test

Source: Internet
Author: User

One, the Android several timers mechanism and the difference.

A: In Android, frequently used timers are mainly implemented in the following ways:

1, using handler and thread of sleep (long) method

Open a thread, sending messages to handler every 1s

public class Mainactivity extends Activity {private Handler myhandler;private static int timemessage=0x01;private int time r=0; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); MyHandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO Auto-generated method Stubif (msg.what==timemessage) {System.out.println ("timer=" + (++timer));}}; New Thread (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubwhile (True) {try {System.out.prin TLN ("carried out"); Thread.Sleep (+); Myhandler.sendemptymessage (timemessage);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}). Start ();}}

2, the use of handler postdelayed (Runnable,long) method

The timer is implemented using a delay of a few seconds to send a message, nested postdelayed ().

public class Mainactivity extends Activity {private Handler myhandler;private static int timemessage = 0x01;private int Ti Mer = 0; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); MyHandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {// TODO auto-generated method stubif (Msg.what = = timemessage) {System.out.println ("timer=" + (++timer));}}}; Myhandler.postdelayed (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubmyhandler.sendemptymessage (Timemessage); myhandler.postdelayed (this, +); if (timer = = 10) { Myhandler.removemessages (Timemessage);}}, 1000);}}
3, using handler and timer, TimerTask combination method

Use The timer class of the schedule () method, every once in a while to do something to implement the timer.

public class Mainactivity extends Activity {private Handler myhandler;private static int timemessage = 0x01;private int Ti Mer = 0;private Timer timerobject; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); MyHandler = new Handler () {@Overridepublic void  Handlemessage (Message msg) {//TODO auto-generated method stubif (Msg.what = = timemessage) {System.out.println ("timer=" + (++timer));}}; Timerobject=new Timer (); Timerobject.schedule (new TimerTask () {//1s, what to do at every 1s @overridepublic void run () {//TODO Auto-generated method Stubmyhandler.sendemptymessage (Timemessage);}}, 1000,1000);}}

Second, using the ListView display name and age, using simpleadapter implementation.

public class Mainactivity extends Activity {private ListView mylistview; @Overrideprotected 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>> ();//each line is displayed as a Map object for ( int i = 0; I < 10; i++) {Map mapdata = new hashmap<string, object> (); Mapdata.put ("Name", "Limi"); Mapdata.put ("Age", "30"); Listdata.add (mapdata);} The second parameter is the ListView row Layout//The third parameter is the key value that holds the MAP element//The fourth parameter corresponds to the key value of the element where the layout is displayed simpleadapter myadpter = new Simpleadapter ( Getapplicationcontext (), Listdata, R.layout.listviewdatalayout, new string[] {"Name", "Age"}, new int[] {r.id.name, r.id . age}); Mylistview.setadapter (Myadpter);}}

Three, Android single case mode application and advantages

Singleton mode ensures that there is only one instance of a class. Make sure that only one instance is produced in your app by using the private constructor.

Because Singleton mode has only one instance in memory, the memory overhead is reduced.

Singleton mode avoids multiple uses of resources, such as when a file is written, because only one instance exists in memory, avoiding simultaneous write operations to the same resource.

Using singleton mode solves the problem of multi-threaded creation at the same time.

The public class Person{private person () {};p rivate the static person zongtong;public the static person Getpersonstance () {if ( Zongtong==null) {synchronized (Person.class) {if (zongtong==null) {zongtong=new person ();}}} return zongtong;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.