Example of jointly updating the UI of service and broadcast

Source: Internet
Author: User
Sa111111 was published at a.m. On in [Android instance] [Copy link] [just look at the landlord] [previous topic] [next topic]

In Android, the UI is updated asynchronously. We usually use handler to start the thread or sendmessage, so the UI can be updated by using service and broadcast, for example:
We create a service:
Package com. Andy
Import Android. App. Service; // introduce related packages
Import Android. content. broadcastreceiver; // introduce the related package
Import Android. content. Context; // introduce related packages
Import Android. content. Intent; // introduce related packages
Import Android. content. intentfilter; // introduce related packages
Import Android. OS. ibinder; // introduce related packages
// Inherit from the subclass of the service
Public class myservice extends Service {
Commandreceiver cmdreceiver;
Boolean flag;
@ Override
Public void oncreate () {// override the oncreate Method
Flag = true;
Cmdreceiver = new commandreceiver ();
Super. oncreate ();
}
@ Override
Public ibinder onbind (intent) {// rewrite the onbind Method
// Todo auto-generated method stub
Return NULL;
}
@ Override
Public int onstartcommand (intent, int flags, int startid) {// override the onstartcommand Method
Intentfilter filter = new intentfilter (); // create an intentfilter object
Filter. addaction ("WYF. WPF. myservice ");
Registerreceiver (cmdreceiver, filter); // register broadcast Receiver
Dojob (); // call the method to start the thread
Return super. onstartcommand (intent, flags, startid );
}
// Method:
Public void dojob (){
New thread (){
Public void run (){
While (FLAG ){
Try {// sleep for a period of time
Thread. Sleep (1000 );
}
Catch (exception e ){
E. printstacktrace ();
}
Intent intent = new intent (); // create an intent object
Intent. setaction ("WYF. WPF. sample_3_6 ");
Intent. putextra ("data", math. Random ());
Sendbroadcast (intent); // send Broadcast
}
}

}. Start ();
}
Private class commandreceiver extends broadcastreceiver {// inherit from the subclass of broadcastreceiver
@ Override
Public void onreceive (context, intent) {// override the onreceive Method
Int cmd = intent. getintextra ("cmd",-1); // get extra information
If (cmd = sample_3_6.cmd_stop_service) {// if the message sent is to stop the service
Flag = false; // stop the thread
Stopself (); // stop the service
}
}
}
@ Override
Public void ondestroy () {// override the 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 related packages
Import Android. content. broadcastreceiver; // introduce the related package
Import Android. content. Context; // introduce related packages
Import Android. content. Intent; // introduce related packages
Import Android. content. intentfilter; // introduce related packages
Import Android. OS. Bundle; // introduce related packages
Import Android. View. view; // introduce related packages
Import Android. View. View. onclicklistener; // introduce the related package
Import Android. widget. Button; // introduce related packages
Import Android. widget. textview; // introduce related packages
// Inherit from the subclass of activity
Public class sample_3_6 extends activity {
Public static final int maid = 0;
Button btnstart; // starts to serve the button object application.
Button btnstop; // stop the service Button Object Application
Textview TV; // textview object application
Datareceiver; // broadcastreceiver object
@ Override
Public void oncreate (bundle savedinstancestate) {// rewrite the oncreate Method
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main); // you can specify the displayed screen.
Btnstart = (button) findviewbyid (R. Id. btnstart );
Btnstop = (button) findviewbyid (R. Id. btnstop );
TV = (textview) findviewbyid (R. Id. TV );
Btnstart. setonclicklistener (New onclicklistener () {// Add a click event listener for the button
@ Override
Public void onclick (view v) {// override the onclick Method
Intent myintent = new intent (sample_3_6.this, WYF. WPF. myservice. Class );
Sample_3_6.this.startservice (myintent); // send intent to start the service
}
});
Btnstop. setonclicklistener (New onclicklistener () {// Add a click event listener for the button
@ Override
Public void onclick (view v) {// override the onclick Method
Intent myintent = new intent (); // create an intent object
Myintent. setaction ("WYF. WPF. myservice ");
Myintent. putextra ("cmd", cmd_stop_service );
Sendbroadcast (myintent); // send Broadcast
}
});
}
Private class datareceiver extends broadcastreceiver {// inherit from the subclass of broadcastreceiver
@ Override
Public void onreceive (context, intent) {// override the onreceive Method
Double Data = intent. getdoubleextra ("data", 0 );
TV. settext ("service data:" + data );
}
}
@ Override
Protected void onstart () {// rewrite the onstart Method
Datareceiver = new datareceiver ();
Intentfilter filter = new intentfilter (); // create an intentfilter object
Filter. addaction ("WYF. WPF. sample_3_6 ");
Registerreceiver (datareceiver, filter); // register broadcast Receiver
Super. onstart ();
}
@ Override
Protected void onstop () {// rewrite the onstop Method
Unregisterreceiver (datareceiver); // cancel registering a broadcast Receiver
Super. onstop ();
}

}

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.