进程内与Service通信

安卓的组件之间一般来说是不会有交流的,但是可以通过BindService的方式实现在Activity与Service的通信。这个通信的媒介就是IBinder。

例如有下面的需求:在Activity内”指挥”一个Service去播放一段音频,当Activity退出时就不再播放,即不支持后台。当用户进行某个操作之后,再点播放,音乐就支持后台。

代码如下:

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example.ahtcfg24.serviceplayerdemo;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import static android.view.View.OnClickListener;

public class MainActivity extends Activity {
public static final String TAG = "Connected";
private Button button, button2;
private Intent intent;
private MyService.MyBinder myService;
private ServiceConnection connection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);

button.setOnClickListener(new ButtonListener());
button2.setOnClickListener(new ButtonListener());

intent = new Intent(MainActivity.this, MyService.class);
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "--->音乐服务已连接");
myService = (MyService.MyBinder) service;
myService.playMedia();//播放
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "--->音乐服务已崩溃");
}
};
}


private class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
bindService(intent, connection, Service.BIND_AUTO_CREATE);
break;
case R.id.button2:
startService(intent);
break;
default:
break;
}
}
}
}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="播放"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp"
android:text="后台播放"/>
</RelativeLayout>

MyService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.ahtcfg24.serviceplayerdemo;

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 {
public static final String TAG = "MyService";
private MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {

Log.i(TAG, "--->onBind");
return new MyBinder();
}

@Override
public void onCreate() {
player = MediaPlayer.create(getApplicationContext(), R.raw.music);
player.setLooping(true);//设置可循环播放
Log.i(TAG, "--->onCreate");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "--->onStartCommand");

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
Log.i(TAG, "--->onDestroy");
super.onDestroy();
player.stop();//Service结束时就停止播放
}

@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "--->onUnbind");

return super.onUnbind(intent);
}

class MyBinder extends Binder {
public void playMedia() {
player.start();//播放
}
}
}

点击查看源代码

参考资料1参考资料2

(本文系作者原创,转载请注明出处)