Implementation of the service dynamic update UI interface in Android

Source: Internet
Author: User

Case: A service sends a request to a remote server, dynamically updates the main program UI interface based on the results returned by the server, and the main program can shut down or restart services in real time.


Register Broadcastreceiver

Register a Broadcastreceiver in the main program activity to receive the broadcast of the Service release.

    1. @Override
    2. protected void OnStart () {//override OnStart method
    3. Datareceiver = new Datareceiver ();
    4. Intentfilter filter = new Intentfilter (); Create a Intentfilter object
    5. Filter.addaction ("Com.justel.serviceBC");
    6. Registerreceiver (datareceiver, filter); //Register broadcast Receiver
    7. Super.onstart ();
    8. }


Stop Service Command

The main program activity can publish broadcasts, which are used to pass data or control information, such as stop service commands, to the back-desk service.

  1. Btnstop.setonclicklistener (new Onclicklistener () {//Add click event Listener for button
  2. @Override
  3. public void onclick (View v) {//override onclick method
  4. Intent myintent = new Intent (); Create a Intent object
  5. Myintent.setaction ("Com.justel.service");
  6. Myintent.putextra ("cmd", cmd_stop_service);
  7. Sendbroadcast (myintent); //Send broadcast
  8. }
  9. });


Receive broadcasts

Background Service registration Broadcastreceiver to accept broadcasts sent by the main program

  1. @Override
  2. public int Onstartcommand (Intent Intent, int flags, int startid) {//override Onstartcommand method /c1>
  3. Intentfilter filter = new Intentfilter (); Create a Intentfilter object
  4. Filter.addaction ("Com.justel.service");
  5. Registerreceiver (cmdreceiver, filter); //Register broadcast Receiver
  6. Dojob (); //Call method to start the thread and do it yourself
  7. return Super.onstartcommand (intent, flags, Startid);
  8. }


Live Send

The background service connects to the server in the Dojob () method and sends broadcasts to the main program in real time.

    1. /**
    2. * Start a sub-purebred and connect to the server, the receiving server returns data. Code slightly ...
    3. */
    4. Object data; //data returned by the server
    5. Intent Intent = new Intent (); Create a Intent object
    6. Intent.setaction ("Com.justel.service");
    7. Intent.putextra ("data", data);
    8. Sendbroadcast (Intent); //Send broadcast

Implementation of the service dynamic update UI interface in Android

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.