两个应用的aidl文件夹中的内容相同,包名为服务器的包名。
IMyAidlInterface.aidl
// IMyAidlInterface.aidl
package com.zhang.aidldemo;
import com.zhang.aidldemo.TimerCallback;
interface IMyAidlInterface {
void setData(String data);
void regCallback(TimerCallback callback);//注册回调接口
void unRegCallback(TimerCallback callback);
}
TimerCallback.aidl
package com.zhang.aidldemo;
interface TimerCallback {
void onTimer(String data);//回调函数
}
MyService
package com.zhang.aidldemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
public class MyService extends Service {
//回调函数管道 列表
private RemoteCallbackList<TimerCallback> callbackList = new RemoteCallbackList<>();
private String data = "原始数据";
private int numIndex = 0;
private boolean serviceRun = false;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return new IMyAidlInterface.Stub() {
//可以将AIDL的IMyAidlInterface理解为特殊的Binder(),在同一个应用与Service互
// 通只需要返回一个Binder()即可,如要传出消息,可在service中建立一个callback
// 接口,用它来把service的数据暴露出去。
// 在不同的应用间则要使用AIDL。
@Override
public void setData(String data) throws RemoteException {
MyService.this.data = data;
}
@Override
public void regCallback(TimerCallback callback) throws RemoteException {
//service onBind 返回 IMyAidlInterface.Stub() 在其中注册回调函数
callbackList.register(callback);
}
@Override
public void unRegCallback(TimerCallback callback) throws RemoteException {
callbackList.unregister(callback);
}
};
}
@Override
public void onCreate() {
super.onCreate();
new Thread() {
@Override
public void run() {
super.run();
serviceRun = true;
for (numIndex = 0; serviceRun; numIndex++) {
//回调处理
int count = callbackList.beginBroadcast();
while (count-- > 0) {
try {
callbackList.getBroadcastItem(count).onTimer(data + ":" + numIndex);
} catch (RemoteException e) {
e.printStackTrace();
}
}
callbackList.finishBroadcast();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
serviceRun = false;
}
}
client的MainActivity
package com.zhang.clientapp;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.zhang.aidldemo.IMyAidlInterface;
import com.zhang.aidldemo.TimerCallback;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private EditText etMsg;
private TextView tvMsg;
private Intent sIntent;
private IMyAidlInterface binder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMsg = (EditText) findViewById(R.id.et_msg);
tvMsg = (TextView) findViewById(R.id.tv_back);
findViewById(R.id.btn_bind).setOnClickListener(this);
findViewById(R.id.btn_unbind).setOnClickListener(this);
findViewById(R.id.btn_send_msg).setOnClickListener(this);
sIntent = new Intent();
sIntent.setComponent(new ComponentName("com.zhang.aidldemo", "com.zhang.aidldemo.MyService"));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_bind:
bindService(sIntent, this, Context.BIND_AUTO_CREATE);
break;
case R.id.btn_unbind:
if (binder != null) {
try {
binder.unRegCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
unbindService(this);
}
break;
case R.id.btn_send_msg:
if (binder != null) {
try {
binder.setData(etMsg.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
binder = IMyAidlInterface.Stub.asInterface(iBinder);
try {
binder.regCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
if (binder != null) {
try {
binder.unRegCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (binder != null) {
try {
binder.unRegCallback(callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
private TimerCallback.Stub callback = new TimerCallback.Stub() {
@Override
public void onTimer(final String data) throws RemoteException {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvMsg.setText(data);
}
});
}
};
}