Basic knowledge of Andriod Service, andriodservice

Source: Internet
Author: User

Basic knowledge of Andriod Service, andriodservice

There are two types of services:

A started service

The enabled service is called by other components.StartService ()Created.

This kind of service can run infinitely and must be calledStopSelf ()Call methods or other componentsStopService ()Method to stop it.

When the service is stopped, the system will destroy it.

A bound service

The Bound service is called when other components (one customer)BindService ().

You can useIBinderInterface to communicate with the service.

The customer canUnbindService ()Method to close this connection.

One service can be bound to multiple customers at the same time. When multiple customers are unbound, the system will destroy the service. (In simple terms, it is attached to the bound Activity)

 

Below is the code:

Package com. jredu. helloworld. activity; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. button; import com. jredu. helloworld. r; import com. jredu. helloworld. service. mySer Vice; public class ServiceActivity extends AppCompatActivity {Button bind; Button unbind; Button start; Button stop; MyService myService; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_service); bind = (Button) findViewById (R. id. bind); unbind = (Button) findViewById (R. id. unbind); start = (Button) findViewById (R. id. Start); stop = (Button) findViewById (R. id. stop); bind. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (ServiceActivity. this, MyService. class); bindService (intent, connection, Context. BIND_AUTO_CREATE); // The first parameter is intent, the second parameter is used to determine the connection status, and the third parameter is bound in one way}); unbind. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View V) {if (myService! = Null) {unbindService (connection); // contact to bind myService = null ;}}); start. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (ServiceActivity. this, MyService. class); startService (intent); // start service}); stop. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (ServiceActivity. This, MyService. class); stopService (intent); // close the service});}/* ServiceConnection is an interface, this interface is used to listen to the connection and disconnection status between the service and the startup source */ServiceConnection connection = new ServiceConnection () {/* when the service is bound to the startup source, call * // ** here IBinder is also involved. After the source and service are successfully linked, you can obtain the IBinder object, * Through the IBinder object, the source and service can be started to complete communication. * In actual development, you usually use the inherited Binder (which implements the IBinder Interface) to implement your own IBinder object. * // @ Override public void onServiceConnected (ComponentName, IBinder service) {myService = (MyService. myBinder) service ). getService () ;}/ * call */@ Override public void onServiceDisconnected (ComponentName name) {}}when the link between the service and the startup source is disconnected due to an exception ){}};}

// A Custom Service Class
Package com. jredu. helloworld. service; import android. app. service; import android. content. intent; import android. media. mediaPlayer; import android. OS. binder; import android. OS. IBinder; import com. jredu. helloworld. r; public class MyService extends Service {MediaPlayer mediaPlayer; public MyService () {}// custom class public class MyBinder extends Binder {public MyService getService () {return MyService. this ;}} @ Override public void onCreate () {super. onCreate (); mediaPlayer = MediaPlayer. create (this, R. raw. bieli); // create an object in the creation method and obtain the resource. Here, we place a song resource. Note that you should change it to English} @ Override public IBinder onBind (Intent intent) {mediaPlayer. start (); return new MyBinder (); // After the binding is successful, a custom object is returned.} @ Override public boolean onUnbind (Intent intent) {mediaPlayer. stop (); return super. onUnbind (intent) ;}@ Override public int onStartCommand (In Tent intent, int flags, int startId) {// This method must be written for services such as a started service; otherwise, mediaPlayer is ineffective. start (); return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {super. onDestroy (); mediaPlayer. stop (); // a started service must call the stop method before the Activity is closed. Otherwise, an error is returned.
    }}

 

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.