Two Methods for refreshing the UI

Source: Internet
Author: User

Two Methods for refreshing the UI

Http://hi.baidu.com/fighter0425/blog/item/acb24612528406c3ac6e7558.html

Recently, I have been considering how to implement real-time temperature measurement and alarm alerts on the Android platform of 6410. Last year I started to learn about Android development, but I still don't know how to refresh the interface, update the collected data continuously. I checked it online today and spent two hours researching it. Haha, I finally figured out what to do. Generally, there are two methods: timer and Message notification, and multi-thread mechanism to actively refresh the interface. Of course, both of these methods are inseparable from the powerful handler in Android. Using this method for UI thread update is indeed a rare choice. Handler won't talk much about it here. There are a lot of online materials that can be viewed at any time.

Method 1:

Package du. helloword;

Import java. util. timer;
Import java. util. timertask;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;

Public class helloactivity extends activity {
/** Called when the activity is first created .*/
Private Final int update_data = 1;
Private textview mydata;
Private timer mtimer;
Private timertask mtimertask;
Private handler mhandler;
Private int I = 0;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mydata = (textview) findviewbyid (R. Id. mydata );
Button button1 = (button) findviewbyid (R. Id. Start );
// Button1.setonclicklistener (start );
Mhandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){

Super. handlemessage (MSG );
Switch (msg. What ){
Case update_data:
I = I + 1;
If (I = 100)
I = 1;
// Mydata. settext ("");
Log. D ("update -- Data", "this is the" + I + "times to print this information ");
Mydata. settext ("this is the" + I + "");
Break;

Default:
Break;
}

}

};
Mtimer = new timer ();
Mtimertask = new timertask () {// timer scheduled processing function
@ Override
Public void run (){
// Todo auto-generated method stub
Mhandler. sendemptymessage (update_data );
}

};
Mtimer. Schedule (mtimertask, 1000,300 0 );
}

}

The following is the simulated AVD


Method 2:

Package du. activity;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. widget. textview;

Public class activity01 extends activity {
Private textview mydata;
Private int I;
Private handler = new handler ();
Private runnable = new runnable (){
Public void run (){
This. Update ();
Handler. postdelayed (this, 1000*2); // interval of 2 seconds
}
Void Update (){
I = I + 1;
If (I = 100)
I = 1;
Mydata. settext ("this is the" + I + "");
}
};
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mydata = (textview) findviewbyid (R. Id. mydata );
Mydata. settext ("initial state ");
Handler. postdelayed (runnable, 1000*2 );
}
@ Override
Protected void ondestroy (){
Handler. removecallbacks (runnable); // stop refreshing
Super. ondestroy ();
}
}

The following are:



Running Effect published on mobile phones

Related Article

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.