After learning about the technology of Android multithreaded programming, it is important to be one of the four components of Android.
Define a service
First look at how to define a service in your project,
Public classMyService extends Service {@Override Public voidonCreate () {super.oncreate (); LOG.D ("MyService","OnCreate"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {LOG.D ("MyService","Command"); returnSuper.onstartcommand (Intent, flags, Startid); } @Override Public voidOnDestroy () {Super.ondestroy (); LOG.D ("MyService","Destroy"); } @Nullable @Override Publicibinder onbind (Intent Intent) {return NULL; }}
Here we rewrite the three methods of OnCreate (), Onstartcommand () and OnDestroy (), which are the most commonly used three methods in each service, where the OnCreate () method is called when the service is created. The Onstartcommand () method is called every time the service is started, and the OnDestroy () method is called when the service is destroyed, and in general, when we destroy the service, we should reclaim those resources that are no longer used in the OnDestroy method.
Each service must be accompanied by your registration in the file before you can use it.
<service android:name=". MyService" />
Such a service is complete.
Start and stop services:
After the service is defined, the next step is how to start and stop the service, and the way to start and stop is mostly done with intent.
Public classMainactivity extends Activity implements View.onclicklistener {PrivateButton StartService; PrivateButton StopService; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); StartService=(Button) Findviewbyid (R.id.start_service); StopService=(Button) Findviewbyid (R.id.stop_service); Startservice.setonclicklistener ( This); Stopservice.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.start_service:intent startintent=NewIntent ( This, MyService.class); StartService (startintent); Break; Caser.id.stop_service:intent stopintent=NewIntent ( This, MyService.class); StopService (stopintent); Break; default: Break; } }
Run effect
What is the difference between onCreate () and Onstartcommand ()?
In fact, the OnCreate () method is called when the service is first created, and the Onstartcommand () method is invoked every time the service is started.
Activities and services for communication
In order to achieve this function we create a dedicated binder object to download the function to manage, modify the code in the MyService,
PrivateDownloadbinder binder=NewDownloadbinder (); classDownloadbinder extends binder{ Public voidstartdownload () {LOG.D ("MyService","Startdownload executed"); } Public intgetprogress () {LOG.D ("MyService","getprogress Execute"); return 0; } }
PrivateButton StartService; PrivateButton StopService; PrivateButton Bindservice; PrivateButton Unbindservice; PrivateMyservice.downloadbinder Downloadbinder; PrivateServiceconnection connection=Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {Downloadbinder=(myservice.downloadbinder) service; Downloadbinder.startdownload (); Downloadbinder.getprogress (); } @Override Public voidonservicedisconnected (componentname name) {}}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); StartService=(Button) Findviewbyid (R.id.start_service); StopService=(Button) Findviewbyid (R.id.stop_service); Startservice.setonclicklistener ( This); Stopservice.setonclicklistener ( This); Bindservice=(Button) Findviewbyid (R.id.bind_service); Unbindservice=(Button) Findviewbyid (R.id.unbinder_service); Bindservice.setonclicklistener ( This); Unbindservice.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.start_service:intent startintent=NewIntent ( This, MyService.class); StartService (startintent); Break; Caser.id.stop_service:intent stopintent=NewIntent ( This, MyService.class); StopService (stopintent); Break; Caser.id.bind_service:intent bindintent=NewIntent ( This, MyService.class); Bindservice (bindintent,connection,bind_auto_create); Break; CaseR.id.unbinder_service:unbindservice (connection); Break; default: Break; } }
We can see big. We first created an anonymous class of Serviceconnection, in which the ONSERVICECONNECTD () method and the Onservicedisconnected () method are rewritten, These two methods are called when the activity is successfully bound to the service and the binding is contacted.
Best practices for services-scheduled tasks performed in the background:
Wirelessly's timing tasks typically have two implementations, one using the timer class provided in the Java API, and one that uses the alarm mechanism of Android, a similar effect that can be achieved in most cases, but the timer has an obvious short board, It is not very suitable for English-speaking tasks that require long periods of time to run in the background.
Basic usage of Android backend service