In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use Service components in Android. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.
The creation of 1:Service
Service means service, and its characteristic is that when you turn on the service, even if you close the app, it will run until the method to stop the service is called. A typical use is a music player.
The creation of Service, like Activity, is best not to inherit and write your own classes. To create the method, right-click your own project-> new- > other- > Android Object- > and then go all the way to the next step (you can change the class name in the middle)
Life cycle of 2:Service
There are two types of life cycle of Service: open service and bind service. If you look at the picture above, you can see it at a glance.
3: start and bind services and shut down services
The first is to start the service, using a button and starting the service in its listening event, as follows
/ / the first button starts the service findViewById (R.id.button1) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {service = new Intent (); / / sets the service service.setClass (MainActivity.this, MyService.class) to be redirected to; / / starts the service startService (service);}}) / / the second button closes the service findViewById (R.id.button2) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {stopService (service);}})
If only the onStartCommand method is called after the service is started, no new service object is created (the constructor is executed only once). As shown in the following figure
The log picture of the normal start-end service is as follows (covered by the watermark). No, no, no. My source code will be uploaded and you can verify it for yourself)
Then there is the second binding service, which is similar to the above, with the following code
/ / bind the third button to the service findViewById (R.id.button3) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {service = new Intent (); / / set the service service.setClass (MainActivity.this, MyService.class) to be redirected to; / / start the service bindService (service, null, Context.BIND_AUTO_CREATE);}}) / / the fourth button unbinds the service findViewById (R.id.button4) .setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {unbindService (null);}})
The binding service method runs in the following order (as shown in the lifecycle)
4: dynamic access to Service
Just now when I started the binding service method, the middle parameter was null. Now let's pass in a parameter for it (because the object to be passed in is an interface, its method must be overridden)
Private ServiceConnection conn = new ServiceConnection () { @ Override public void onServiceDisconnected (ComponentName name) { } @ Override public void onServiceConnected (ComponentName name IBinder service) {}} FindViewById (R.id.button1) .setOnClickListener (new OnClickListener () { @ Override public void onClick (View v) {Intent service = new Intent (MainActivity.this) MyService.class) BindService (service, conn, Context.BIND_AUTO_CREATE);}})
Go back to the code page of Service, write a play method in Service and log it so that we can see the result, then write an inner class that inherits binder and write a method to call the play () method of Service, and finally return a binder object in the onbind method
@ Overridepublic IBinder onBind (Intent intent) {return new MyBinder ();} class MyBinder extends Binder {public void start () {play () }} public void play () {Log.e ("MyService", "play ()");}
After going back to the Activity code, get the Mybinder object in the onServiceConnected () method in the conn interface, and call the object's start () method as follows
Private ServiceConnection conn = new ServiceConnection () {@ Override public void onServiceDisconnected (ComponentName name) {} @ Override public void onServiceConnected (ComponentName name IBinder service) {/ / get the object MyBinder binder = (MyBinder) service of Mybinder in Service / / call the method in Mybinder, which contains the method binder.start ();} in Service.
The great task has been completed. The log print result is as follows
The above is all the content of the article "how to use Service components in Android". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.