android BindService介绍

 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 }



原文地址:https://www.cnblogs.com/shaoyangjiang/p/2384188.html