一、AIDL文件:
// IZFTtsAidlInterface.aidl
package com.example.raisesail.zftts;
interface IZFTtsAidlInterface {
void ZFSpeak(String str);
}
二、实现类:
public class MyBinder extends IZFTtsAidlInterface.Stub{
@Override
public void ZFSpeak(String str) throws RemoteException
{
speak(str);
return;
}
}
在服务端实例化,当客户端绑定服务时,返回binder代理,即:
private MyBinder binder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
三、客户端绑定服务:
//zftts begin
private IZFTtsAidlInterface mttsinterface;
private ServiceConnection zftts_conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mttsinterface = IZFTtsAidlInterface.Stub.asInterface(service);
if(mttsinterface != null){
Log.i("zyl log 0228","SystemBars---------------onServiceConnected successed");
//zftts test
/*try {
mttsinterface.ZFSpeak("展帆语音测试180302");
} catch (RemoteException e) {
e.printStackTrace();
}*/
if(mStatusBar != null){
mStatusBar.setZFTtsinterface(mttsinterface);
}
}
}
//client 和service连接意外丢失时,会调用该方法
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
//end
Context.bindService(intent, zftts_conn, Context.BIND_AUTO_CREATE);
1.获得代理,可与服务端通信
2.注意aidl文件需要与服务端包名类名一致
四、关于命令中心启动服务:
private String doExecCmd(String cmd){
Runtime mRuntime = Runtime.getRuntime();
try {
java.lang.Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}