Android Learning Path--service and activity communication

Source: Internet
Author: User

a) code for the Mainactivity class
1 PackageCom.example.cms.service;2 3 ImportAndroid.app.Service;4 ImportAndroid.content.ComponentName;5 Importandroid.content.Intent;6 Importandroid.content.ServiceConnection;7 ImportAndroid.os.IBinder;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.View; One ImportAndroid.widget.Button; A ImportAndroid.widget.Toast; - - Public classMainactivityextendsappcompatactivity { the PrivateButton bind,unbind,getservicestatus; - Bindservice.mybinder Binder; - PrivateServiceconnection conn=Newserviceconnection () { - @Override + Public voidonserviceconnected (componentname name, IBinder service) {//service is returned by the Onbinder () method in the service. -SYSTEM.OUT.PRINTLN ("----Service Connected-----"); +Binder=(bindservice.mybinder) service; A } at @Override - Public voidonservicedisconnected (componentname name) { -SYSTEM.OUT.PRINTLN ("----Service disconnted----"); - } - }; - in @Override - protected voidonCreate (Bundle savedinstancestate) { to Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -Bind=(Button) Findviewbyid (r.id.bind); theUnbind=(Button) Findviewbyid (r.id.unbind); *getservicestatus=(Button) Findviewbyid (r.id.getservicestatus); $ FinalIntent intent=NewIntent ( This, Bindservice.class);Panax NotoginsengBind.setonclicklistener (NewView.onclicklistener () { - @Override the Public voidOnClick (View v) { + //binding specified service A Bindservice (Intent,conn, service.bind_auto_create); the } + }); -Unbind.setonclicklistener (NewView.onclicklistener () { $ @Override $ Public voidOnClick (View v) { - //Contact Binding - Unbindservice (conn); the } - });WuyiGetservicestatus.setonclicklistener (NewView.onclicklistener () { the @Override - Public voidOnClick (View v) { WuToast.maketext (mainactivity. This, the count value for Service is: "+Binder.getcount (), Toast.length_short). Show (); - } About }); $ } - -}

B) Service Class code
 PackageCom.example.cms.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;Importandroid.support.annotation.Nullable;/*** Created by CMS on 2016/3/28.*/ Public classBindserviceextendsService {Private intcount; Private Booleanquit=false; PrivateMybinder binder=NewMybinder (); classMybinderextendsbinder{ Public intGetCount () {returncount; }} @Override Publicibinder onbind (Intent Intent) {System.out.print ("Service is binded"); returnBinder; } @Override Public voidonCreate () {Super. OnCreate (); System.out.print ("Service is Created"); NewThread () { Public voidrun () { while(!quit) {                    Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } Count++;    }}}.start (); } @Override Public BooleanOnunbind (Intent Intent) {System.out.println ("Service is unbinded"); return true; } @Override Public voidOnDestroy () {Super. OnDestroy ();  This. quit=true; System.out.println ("Service ID destroyed"); }}

Android Learning Path--service and activity communication

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.