Common Methods for Bound Services

Source: Internet
Author: User

Most of the instances that use the service to play music use the SDK,

MainActivity:

Package com. example. androidtest; import com. example. androidtest. myService. localBinder; import android. OS. bundle; import android. OS. IBinder; import android. app. activity; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. serviceConnection; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {protected myService mService; // It is used to save the myService object returned by sevice and call the protected boolean mBound method provided in Service; // used to determine the binding status private ServiceConnection mConnection = new ServiceConnection () {// as the bindService parameter. After binding, onServiceConnected is called and the returned IBinder object is received, this object can be used to obtain the data to be provided by the service. In this example, because the getService method is added to LocalBinder in the service, it can be used to obtain the myService object @ Override public void onServiceConnected (ComponentName className, IBinder service) {LocalBinder binder = (LocalBinder) service; mService = binder. getService (); mBound = true;} @ Override public void onServiceDisconnected (ComponentName arg0) {// called after unbindService for clearing the resource mBound = false; mService. stopMusic () ;};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bindService (new Intent (this, myService. class), mConnection, Context. BIND_AUTO_CREATE); // bind the Service Button startButton = (Button) findViewById (R. id. button1); Button pauseButton = (Button) findViewById (R. id. button2); Button stopButton = (Button) findViewById (R. id. button3); startButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {mService. playMusic (); // TODO Auto-generated method stub}); pauseButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {mService. pauseMusic (); // TODO Auto-generated method stub}); stopButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {mService. stopMusic () ;}}) ;}@ Override protected void onDestroy () {// TODO Auto-generated method stub super. onDestroy (); if (mBound = true) {unbindService (mConnection); // unbind }}}

MyService:

Package com. example. androidtest; import android. app. service; import android. content. intent; import android. media. mediaPlayer; import android. OS. binder; import android. OS. IBinder; import android. util. log; public class myService extends Service {private LocalBinder localBinder = new LocalBinder (); private MediaPlayer mediaPlayer; // create a MediaPlayer object that uses raw/music as the playback file, it can be used to play music @ Override public IBinder onBind (Intent intent) {// TODO Auto-generated method stub mediaPlayer = MediaPlayer. create (myService. this, R. raw. music); return localBinder;} // create an internal class as the onBind return value, used to obtain the server carrier public class LocalBinder extends Binder {myService getService () from the Client () {// directly return the myService object. I personally think this method is relatively simple. You can also create other methods that need to be returned to the client to communicate with return myService. this ;}} // Method for calling the client: public void playMusic () {mediaPlayer. start ();} public void pauseMusic () {mediaPlayer. pause ();} public void stopMusic () {mediaPlayer. stop ();}}


This article from the "wenjian minor notes" blog, please be sure to keep this http://wenjianboy.blog.51cto.com/7389753/1298952

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.