The second is to start the Service by binding it. First look at the flowchart.
Click to view the chart
The biggest difference between the binding method and the method described in section 1 is that an Activity can be associated with a Service. When the associated Activity ends, the corresponding Service also stops, at the same time, in the bound Activity, we can also call back the methods we have defined in the Service. Here we use this. bindService (intent, myServiceConnection, Context. BIND_AUTO_CREATE); to start the Service. After the Service is created and bound to the Activity, it calls back the ServiceConnection () defined by us and returns the IBinder interface. Then, we can call the methods in the Service. At this time, the Activity is bound to the Service, and the Activity exits after exiting the Service. The Service statement is as follows. intent filter is used to filter the received Service.
- <service android:enabled="true" android:name=".service.mediaplayer.BindMusicService">
- <intent-filter>
- <action android:name="com.androidtest.service.mediaplayer.BindMusicService" />
- </intent-filter>
- </service>
The source code for this section can be downloaded here: http://down.51cto.com/data/326385