BindService中使用bindService()方法來綁定服務,調用者和綁定者綁在一起,調用者一旦退出服務也就終止了【onCreate()->onBind()->onUnbind()->onDestroy()】。
由於Android 中的Service使用了onBind 的方法去綁定服務,返回一個Ibinder對象進行操作,而我們要擷取具體的Service方法的內容的時候,我們需要Ibinder對象返回具體的Service對象才能操作,所以說具體的Service對象必須首先實現Binder對象,這個樣子的話我們才能利用bindService的方法對Service進行綁定,擷取Binder對象之後擷取具體的Service對象,然後才擷取Service中的方法等等。所以我們需要注意的是bindService的方式去綁定服務擷取的必定是實現了Binder的對象,所以這是我們必須使用Binder的方式去擷取Service的方式而不是直接使用Service的類,這個是Android內部實現所約束的。
方法過程如下:
Intent intent = new Intent(MainActivity.this,BindService.class)->建立了BindService對象->建立了MyBinder對象
->bindService(intent, conn, Context.BIND_AUTO_CREATE);->onBind()函數 -----傳遞MyBinder對象------->onServiceConnected()
--> 通過傳遞的Binder對象擷取剛剛和Binder對象對應的BindService 對象 -->調用Service中定義的方法。
這個其中必須通過Binder對象,因為是通過Binder對象來傳遞的,通過Binder對象擷取Service對象,然後擷取所需的服務,所以Service必須實現Binder,以便傳遞和使用。
示範一個播放器的小例子,是用bindService的方法來進行的
第一步,編寫AndroidMainFest.xml檔案
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>
第二步:編寫PlayerActivity.java檔案
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 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的作用相當於管道串連作用,連結playerActivity和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 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 }
第三步:編寫 MusicService.java檔案
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 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 //這個方法是在MusicService被綁定到其他應用程式上時被調用,
20 //這個IBinder對象和應用程式的OnServiceConnecred方法中傳入的Ibinder是同個事物,
21 //應用程式和MusicService就靠這個IBinder進行通訊
22 @Override
23 public IBinder onBind(Intent 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 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 }