Android Development Bindservice () listens for service internal status

Source: Internet
Author: User

Based on the Bindservice () Communication of Android development , the Bindservice () method is implemented to listen for service internal state.

Implements the listening service internal state, using a callback mechanism

1. First implement an interface

1      Public Static  Interface callback{2         void OnDataChange (String data); 3     }

2.

1     Private CallBack callback=null; 2      Public void Setcallback (CallBack CallBack) {3         this. callBack = callBack; 4     }5public       CallBack getcallback () {6          return  callBack; 7     }

3.

1 @Override2      Public voidonCreate () {3         //TODO auto-generated Method Stub4         Super. OnCreate ();5running=true;6         NewThread () {7              Public voidrun () {8                 intI=0;9                  while(running) {Teni++; OneString str=i+ ":" +data; ALOG.D ("Learnservice", str); -                     if(callback!=NULL) {       - Callback.ondatachange (str);  the                     } -                     Try { -Sleep (1000); -}Catch(interruptedexception e) { +                         //TODO auto-generated Catch block - e.printstacktrace (); +                     } A                 } at             }; - }.start (); -}

The 4.MyBinder provides methods to return Bindservice instances.

1      Public classMybinderextendsbinder{2          Public voidsetData (String data) {3MyService. This. data=data;4         }5          PublicMyService GetService () {6             returnMyService. This;7         }8}

5.

1 @Override2          Public voidonserviceconnected (componentname name, IBinder service) {3             //TODO auto-generated Method Stub4Binder=(mybinder) service;5Binder.getservice (). Setcallback (NewMyservice.callback () {6                 7 @Override8                  Public voidOnDataChange (String data) {9                     //TODO auto-generated Method StubTenMessage msg=NewMessage (); OneBundle bundle=NewBundle (); ABundle.putstring ("Data", data); - Msg.setdata (bundle); - handler.sendmessage (msg); the                 } -             }); -         } -};

6. Via handler, put data on the UI thread and update the UI thread

1     Private Handler handler=New  Handler () {2public         void  handlemessage ( Android.os.Message msg) {3             Tvout.settext (Msg.getdata (). getString ("Data")); 4         }; 5     };

Android Development Bindservice () listens for service internal status

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.