AIDL Demo

一、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);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值