Basic use of Handler (Android review)

Source: Internet
Author: User


I. Basic points of knowledge

1, Intent Intent = new Intent ();//Open browser's
Intent.setaction (Intent.action_view);
Intent.setdata (Uri.parse ("http://www.baidu.com"));


2, Systemclock.sleep (20000);//Sleep 20 seconds, used to disguise to win a


3, time-consuming operations should be done in sub-threads
Networked access to data
Copies of large files
All need to be placed on child threads to manipulate


The OnCreate () button clicks on the callback event, and the action shown is run inside the main thread. The UI thread.


4, the use of handler
New Handler ();


Message msg = new Message ();
Msg.what = update_display;//Unique identification of the set message
Msg.obj = i;
Mhandler.sendmessage (msg)



Second, the sample code

Package Com.example.handlertest;import Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.os.systemclock;import android.app.activity;import Android.view.menu;import Android.view.View;import Android.widget.textview;public class Mainactivity extends Activity {public TextView tv_num;public static final int UPDATE _number = 0;public int i = 0;public Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {if ( Msg.what = = update_number) {int i = (Integer) msg.obj;tv_num.settext (i + "");}};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); tv_num = (TextView) Findviewbyid (r.id.tv_number);} public void Add (view view) {new Thread () {@Overridepublic void run () {Super.run (), while (I <) {Systemclock.sleep ( ); i + = 1; Message msg = new Message (); msg.what = Update_number;msg.obj = I;handler.sendmessage (msg);}}}. Start ();} @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


Third, the source code download

http://download.csdn.net/detail/caihongshijie6/7798627


Four




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.