定义aidl接口
package com.xykj.musicserviceapp;
import com.xykj.musicserviceapp.Music;
interface IMusicInterface {
//in out inout引用类型
void init(in List<Music> list);
void play(int index);
void pause();
}
注意:自定义类型数据要传递,那么该类型必须要序列化,并且要有aidl文件描述,如Music.java那么需要Music.aidl来描述
parcelable Music;
服务进程实现aidl定义的功能
public class MusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends IMusicInterface.Stub{
@Override
public void init(List<Music> list) throws RemoteException {
Log.e("m_tag","MusicService init:"+list.size());
}
@Override
public void play(int index) throws RemoteException {
Log.e("m_tag","MusicService play:"+index);
}
@Override
public void pause() throws RemoteException {
Log.e("m_tag","MusicService pause");
}
}
}
调用端的进程可以从服务进程这边拷贝aidl文件(或者拷贝aidl生成的java文件),绑定该实现服务即可通讯
...
@Override
protected void onResume() {
super.onResume();
Intent it = new Intent();
ComponentName cn = new ComponentName("com.xykj.musicserviceapp",
"com.xykj.musicserviceapp.MusicService");
it.setComponent(cn);
bindService(it, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
unbindService(conn);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mInterface = IMusicInterface.Stub.asInterface(service);
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
使用远程服务的功能
if (null != mInterface) {
try {
mInterface.play(1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
##### 进程通讯的回调
可以定义aidl回调接口
package com.xykj.musicserviceapp;
interface IMusicCallback {
void onDuration(int duration);
void onCurrentPosition(int position);
}
在功能aidl上添加注册回调以及取消注册回调方法
package com.xykj.musicserviceapp;
import com.xykj.musicserviceapp.Music;
import com.xykj.musicserviceapp.IMusicCallback;
interface IMusicInterface {
//in out inout引用类型
void init(in List<Music> list);
void play(int index);
void pause();
void registCallback(IMusicCallback callback);
void unRegistCallback(IMusicCallback callback);
}
在远程服务端使用RemoteCallbackList管理回调
private RemoteCallbackList<IMusicCallback> callbackList;
@Override
public void onCreate() {
super.onCreate();
callbackList = new RemoteCallbackList<>();
}
...
@Override
public void registCallback(IMusicCallback callback) throws RemoteException {
callbackList.register(callback);
}
@Override
public void unRegistCallback(IMusicCallback callback) throws RemoteException {
callbackList.unregister(callback);
}
...
返回数据(使用回调)
private void sendDuration(int duration) throws RemoteException {
//准备返回数据,得到监听数量
int count = callbackList.beginBroadcast();
for(int i = 0 ; i < count ; i++){
IMusicCallback callback = callbackList.getBroadcastItem(i);
callback.onDuration(duration);
}
callbackList.finishBroadcast();
}
#### 应用多进程
在配置manifest中使用android:process
<service
android:name=".MyService2"
android:enabled="true"
android:exported="true"
android:process=":test"></service>
<!-- 私有进程,名字以冒号开头,全称是前面加上应用包名:com.xykj.processdemo:test -->
<!-- 全局进程,小写字母用点连接 如:com.xykj.testprocess -->
<service
android:name=".MyService2"
android:enabled="true"
android:exported="true"
android:process="com.xykj.testprocess"/>
注意:多进程应用,不同进程下的static单例会失效,不能共享,可以使用aidl方式通讯,数据库可以使用ContentProvider
package com.xykj.musicserviceapp;
import com.xykj.musicserviceapp.Music;
interface IMusicInterface {
//in out inout引用类型
void init(in List<Music> list);
void play(int index);
void pause();
}
注意:自定义类型数据要传递,那么该类型必须要序列化,并且要有aidl文件描述,如Music.java那么需要Music.aidl来描述
parcelable Music;
服务进程实现aidl定义的功能
public class MusicService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
class MyBinder extends IMusicInterface.Stub{
@Override
public void init(List<Music> list) throws RemoteException {
Log.e("m_tag","MusicService init:"+list.size());
}
@Override
public void play(int index) throws RemoteException {
Log.e("m_tag","MusicService play:"+index);
}
@Override
public void pause() throws RemoteException {
Log.e("m_tag","MusicService pause");
}
}
}
调用端的进程可以从服务进程这边拷贝aidl文件(或者拷贝aidl生成的java文件),绑定该实现服务即可通讯
...
@Override
protected void onResume() {
super.onResume();
Intent it = new Intent();
ComponentName cn = new ComponentName("com.xykj.musicserviceapp",
"com.xykj.musicserviceapp.MusicService");
it.setComponent(cn);
bindService(it, conn, Context.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
unbindService(conn);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mInterface = IMusicInterface.Stub.asInterface(service);
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
使用远程服务的功能
if (null != mInterface) {
try {
mInterface.play(1);
} catch (RemoteException e) {
e.printStackTrace();
}
}
##### 进程通讯的回调
可以定义aidl回调接口
package com.xykj.musicserviceapp;
interface IMusicCallback {
void onDuration(int duration);
void onCurrentPosition(int position);
}
在功能aidl上添加注册回调以及取消注册回调方法
package com.xykj.musicserviceapp;
import com.xykj.musicserviceapp.Music;
import com.xykj.musicserviceapp.IMusicCallback;
interface IMusicInterface {
//in out inout引用类型
void init(in List<Music> list);
void play(int index);
void pause();
void registCallback(IMusicCallback callback);
void unRegistCallback(IMusicCallback callback);
}
在远程服务端使用RemoteCallbackList管理回调
private RemoteCallbackList<IMusicCallback> callbackList;
@Override
public void onCreate() {
super.onCreate();
callbackList = new RemoteCallbackList<>();
}
...
@Override
public void registCallback(IMusicCallback callback) throws RemoteException {
callbackList.register(callback);
}
@Override
public void unRegistCallback(IMusicCallback callback) throws RemoteException {
callbackList.unregister(callback);
}
...
返回数据(使用回调)
private void sendDuration(int duration) throws RemoteException {
//准备返回数据,得到监听数量
int count = callbackList.beginBroadcast();
for(int i = 0 ; i < count ; i++){
IMusicCallback callback = callbackList.getBroadcastItem(i);
callback.onDuration(duration);
}
callbackList.finishBroadcast();
}
#### 应用多进程
在配置manifest中使用android:process
<service
android:name=".MyService2"
android:enabled="true"
android:exported="true"
android:process=":test"></service>
<!-- 私有进程,名字以冒号开头,全称是前面加上应用包名:com.xykj.processdemo:test -->
<!-- 全局进程,小写字母用点连接 如:com.xykj.testprocess -->
<service
android:name=".MyService2"
android:enabled="true"
android:exported="true"
android:process="com.xykj.testprocess"/>
注意:多进程应用,不同进程下的static单例会失效,不能共享,可以使用aidl方式通讯,数据库可以使用ContentProvider