Example of jointly updating the UI of Android service and broadcast

Source: Internet
Author: User
Tags service flag

In Android, the UI is updated asynchronously. We usually use handler to start the thread or sendmessage, so the UI can be updated using service and broadcast. For example: we create a service:

Package COM. andyimport android. app. service; // introduce the related package import android. content. broadcastreceiver; // introduce the related package import android. content. context; // introduce the related package import android. content. intent; // introduce the related package import android. content. intentfilter; // introduce the related package import android. OS. ibinder; // introduce related packages // The public class myservice extends service that inherits from the service subclass {commandreceiver cmdreceiver; Boolean flag; @ override public void oncreate () {// override the oncreate method flag = true; cmdreceiver = newcommandreceiver (); super. oncreate () ;}@ override public ibinder onbind (intent) {// override onbind method // todo auto-generated methodstub return NULL;} @ override public int onstartcommand (intent, intflags, int startid) {// override the onstartcommand method intentfilter filter = newintentfilter (); // create the intentfilter object filter. addaction ("WYF. WPF. myservice "); registerreceiver (cmdreceiver, filter); // register broadcast javaser dojob (); // call the method to start the returnsuper thread. onstartcommand (intent, flags, startid);} // method: Public void dojob () {newthread () {publicvoid run () {While (FLAG) {try {// thread for a sleep period. sleep (1000);} catch (partition tione) {e. printstacktrace ();} intent = newintent (); // create intent object intent. setaction ("WYF. WPF. sample_3_6 "); intent. putextra ("data", math. random (); sendbroadcast (intent); // send broadcast }}}. start ();} private class commandreceiver extendsbroadcastreceiver {// inherit from the subclass of broadcastreceiver @ override public void onreceive (contextcontext, intent) {// override onreceive method int cmd = intent. getintextra ("cmd",-1); // obtain extra information if (cmd = sample_3_6.cmd_stop_service) {// if the message sent is stop service flag = false; // stop thread stopself (); // stop service }}@ override public void ondestroy () {// override ondestroy method this. unregisterreceiver (cmdreceiver); // cancel the registered commandreceiver super. ondestroy ();}}

Create an activity

Package COM. andy; // declare the package statement import android. app. activity; // introduce the related package import android. content. broadcastreceiver; // introduce the related package import android. content. context; // introduce the related package import android. content. intent; // introduce the related package import android. content. intentfilter; // introduce the related package import android. OS. bundle; // introduce the related package import android. view. view; // introduce the related package import android. view. view. onclicklistener; // introduce the related package import android. widget. button; // import related packages to Android. widget. textview; // introduce the relevant package // inherit the public class sample_3_6 extends activity from the subclass of the activity {public static final int completion _stop_service = 0; buttonbtnstart; // start the service button object to apply the button btnstop; // stop the service button object application textview TV; // textview object application datareceiverdatareceiver; // broadcastreceiver object @ override public void oncreate (bundlesavedinstancestate) {// rewrite the oncreate method super. oncreate (savedinstancestate); setcontentview (R. layout. main); // set the btnstart = (button) findviewbyid (R. id. btnstart); btnstop = (button) findviewbyid (R. id. btnstop); TV = (textview) findviewbyid (R. id. TV); btnstart. setonclicklistener (newonclicklistener () {// Click Event listener @ override publicvoid onclick (view v) {// override onclick method intent myintent = new intent (sample_3_6.this, WYF. WPF. myservice. class); sample_3_6.this.startservice (myintent); // send intent to start service}); btnstop. setonclicklistener (newonclicklistener () {// Add a click event listener @ override publicvoid onclick (view v) {// rewrite the onclick method intent myintent = newintent (); // create the intent object myintent. setaction ("WYF. WPF. myservice "); myintent. putextra ("cmd", cmd_stop_service); sendbroadcast (myintent); // send broadcast }});} private class datareceiver extendsbroadcastreceiver {// inherit from the subclass of broadcastreceiver @ override public void onreceive (contextcontext, intent) {// override onreceive method doubledata = intent. getdoubleextra ("data", 0); TV. settext ("service data:" + Data) ;}@ override protected void onstart () {// rewrite onstart method datareceiver = newdatareceiver (); intentfilter filter = newintentfilter (); // create an intentfilter object filter. addaction ("WYF. WPF. sample_3_6 "); registerreceiver (datareceiver, filter); // register broadcastreceiver super. onstart () ;}@ override protected void onstop () {// override onstop method unregisterreceiver (datareceiver); // cancel registration of broadcastreceiver super. onstop ();}}

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.