Bindservice uses the bindservice () method to bind the service. When the caller and the biner are bound together, the consumer terminates the [oncreate ()-> onbind ()-> onunbind () -> ondestroy ()].
Because the service in Android uses the onbind method to bind the service, an ibinder object is returned for operation. When we want to obtain the content of the specific service method, we need to return a specific service object for the ibinder object to operate. Therefore, the specific service object must first implement the binder object. In this way, we can use the bindservice method to bind the service, after obtaining the binder object, obtain the specific service object, and then obtain the methods in the service. Therefore, we need to note that the bindservice method to bind the Service must implement the binder object, therefore, we must use the binder method to obtain the service instead of directly using the service class. This is restricted by the internal implementation of Android.
The procedure is as follows:
Intent intent = new intent (mainactivity. This, bindservice. Class)-> New bindservice object-> New mybinder object
-> Bindservice (intent, Conn, context. bind_auto_create);-> onbind () function ----- pass the mybinder object -------> onserviceconnected ()
--> Get the bindservice object corresponding to the binder object through the passed binder object --> call the method defined in service.
This must be passed through the binder object, because it is passed through the binder object, get the service object through the binder object, and then get the required service, so the Service must implement the binder for transmission and use.
A small example of a player is presented using the bindservice method.
Step 1: Compile the androidmainfest. xml file
View code
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="cn.edu.zwu.tel"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:name=".MyMusicPlayer1Activity"
14 android:label="@string/app_name" >
15 <intent-filter>
16 <action android:name="android.intent.action.MAIN" />
17 <category android:name="android.intent.category.LAUNCHER" />
18 </intent-filter>
19 </activity>
20 <activity android:name=".PlayerActivity">
21 </activity>
22 <service android:enabled="true" android:name=".MusicService">
23 </service>
24 </application>
25
26 </manifest>
Step 2: Compile the playeractivity. Java File
View code
1 import Android. App. activity;
2 Import Android. content. componentname;
3 Import Android. content. context;
4 Import Android. content. intent;
5 import Android. content. serviceconnection;
6 Import Android. OS. Bundle;
7 Import Android. OS. ibinder;
8 Import Android. View. view;
9 Import Android. widget. imagebutton;
10 Import Android. widget. seekbar;
11
12
13 public class playeractivity extends activity {
14
15 private musicservice;
16 imagebutton imagebuttonstop;
17 imagebutton imagebuttonnext;
18 imagebutton imagebuttonplay;
19 imagebutton imagebuttonpre;
20 imagebutton imagebuttonrepeat;
21 seekbar musicseekbar;
22
23 Boolean isplaying = false;
24 Boolean isbound = false;
25 // conn serves as a pipe connection, linking playeractivity and musicservice,
26 private serviceconnection conn = new serviceconnection (){
27 @ override
28 public void onserviceconnected (componentname name, ibinder Service)
29 {
30 musicservice = (musicservice. mybinder) Service). getservice ();
31}
32
33 @ override
34 public void onservicedisconnected (componentname name)
35 {
36 musicservice = NULL;
37
38}
39 };
40
41 @ override
42 Public void oncreate (bundle savedinstancestate ){
43 super. oncreate (savedinstancestate );
44 setcontentview (R. layout. Player );
45
46 imagebuttonstop = (imagebutton) findviewbyid (R. Id. imagebuttonstop );
47 imagebuttonnext = (imagebutton) findviewbyid (R. Id. imagebuttonnext );
48 imagebuttonplay = (imagebutton) findviewbyid (R. Id. imagebuttonplay );
49 imagebuttonpre = (imagebutton) findviewbyid (R. Id. imagebuttonpre );
50 imagebuttonrepeat = (imagebutton) findviewbyid (R. Id. imagebuttonrepeat );
51 musicseekbar = (seekbar) findviewbyid (R. Id. musicseekbar );
52
53
54 final intent = new intent (playeractivity. This, musicservice. Class );
55 if (! Isbound)
56 {
57 bindservice (intent, Conn, context. bind_auto_create );
58 isbound = true;
59}
60
61 imagebuttonplay. setonclicklistener (New View. onclicklistener ()
62 {
63 @ override
64 public void onclick (view V)
65 {
66 If (! Isplaying ){
67 try {
68 musicservice. Play ();
69} catch (exception e ){
70 E. printstacktrace ();
71}
72 imagebuttonplay. setbackgroundresource (R. drawable. pause_button );
73 isplaying = true;
74
75} else {
76 try {
77 musicservice. Pause ();
78} catch (exception e ){
79 E. printstacktrace ();
80}
81 imagebuttonplay. setbackgroundresource (R. drawable. play_button );
82 isplaying = false;
83}
84}
85 });
86
87
88 imagebuttonstop. setonclicklistener (New View. onclicklistener ()
89 {
90 @ override
91 Public void onclick (view V)
92 {
93 musicservice. Stop ();
94 If (isbound)
95 {
96 unbindservice (conn );
97 isbound = false;
98 musicservice = NULL;
99}
100
101 playeractivity. This. Finish ();
102}
103 });
104
105}
106}
Step 3: Compile the musicservice. Java File
View code
1 import Android. App. Service;
2 Import Android. content. intent;
3 Import Android. Media. mediaplayer;
4 Import Android. OS. Binder;
5 import Android. OS. ibinder;
6
7 public class musicservice extends Service
8 {
9 private final mybinder = new mybinder ();
10 public static mediaplayer mplayer;
11
12 public class mybinder extends Binder
13 {
14 musicservice getservice ()
15 {
16 return musicservice. This;
17}
18}
19 // this method is called when musicservice is bound to another application,
20 // The ibinder object and the ibinder passed in the onserviceconnecred method of the application are the same thing,
21 // The application and musicservice communicate with this ibinder.
22 @ override
23 public ibinder onbind (intent)
24 {
25 mplayer = mediaplayer. Create (getapplicationcontext (), R. Raw. Wind );
26 return mybinder;
27}
28
29 @ override
30 public void oncreate ()
31 {
32 // mplayer = mediaplayer. Create (getapplicationcontext (), R. Raw. Wind );
33}
34
35 @ override
36 Public Boolean onunbind (intent)
37 {
38 return false;
39}
40
41 @ override
42 Public void ondestroy (){
43 super. ondestroy ();
44}
45
46 Public void play ()
47 {
48 mplayer. Start ();
49}
50
51 public void pause ()
52 {
53 mplayer. Pause ();
54}
55 public void stop ()
56 {
57 mplayer. Stop ();
58}
59
60}