进程中简单的AIDL通信实现

服务端:
1创建一个aidl文件,在其中定义你想要的方法

package com.zhy.calc.aidl; 
interface ICalcAIDL  
{  
    int add(int x , int y);  
    int min(int x , int y );  
}
2创建一个自已的服务,并在该服务中实现aidl暴露出的接口方法

package com.example.service;

import com.zhy.calc.aidl.ICalcAIDL;

import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class CalcSeriver extends Service {

private static final String TAG = "server";

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Log.e(TAG, "OnCreate");
    super.onCreate();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}

public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
}

public boolean onUnbind(Intent intent) {
    Log.e(TAG, "onUnbind");
    return super.onUnbind(intent);
}public void onRebind(Intent intent) {
    Log.e(TAG, "onRebind");
    super.onRebind(intent);
}

private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub() {
    public int add(int x, int y) throws android.os.RemoteException {
        return x + y;
    }

    public int min(int x, int y) throws android.os.RemoteException {
        // TODO Auto-generated method stub
        return x - y;
    }
};

}
3在配置文件中注册服务的启动

<service android:name="com.example.service.CalcSeriver">
            <intent-filter >
                <action android:name="com.cao.aidl.calc"/>
                <action android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

客户端
1新建一个aidl文件,该文件的包名和定义要与服务端的一样

package com.zhy.calc.aidl;
interface ICalcAIDL
{
int add(int x , int y);
int min(int x , int y );
}
2在主页面写自已的逻辑

public class MainActivity extends ActionBarActivity {

    private ICalcAIDL mCalcAidl;
    private ServiceConnection mServiceConn=new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
             Log.e("client", "onServiceDisconnected");  
                mCalcAidl = null; 
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
             Log.e("client", "onServiceConnected");
             mCalcAidl = ICalcAIDL.Stub.asInterface(service); 
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//4个方法都是在xml文件中button的点击事件
    public void bindService(View view){
        Intent intent=new Intent();
        intent.setAction("com.cao.aidl.calc");
        bindService(intent,mServiceConn,Context.BIND_AUTO_CREATE);
    }
    public void unbindService(View view){
        unbindService(mServiceConn);
    }
    public void addInvoked(View view ) throws RemoteException{
        if (mCalcAidl!=null) {
            int addRes=mCalcAidl.add(12, 12);
            Toast.makeText(this, addRes+"", Toast.LENGTH_SHORT).show();
        } else {
             Toast.makeText(this, "服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)  
             .show();
        }
    }
    public void minInvoked(View view)throws RemoteException{
        if(mCalcAidl!=null){
            int minRes=mCalcAidl.min(50, 12);
            Toast.makeText(this,minRes+"" , Toast.LENGTH_SHORT).show();
        }else {
             Toast.makeText(this, "服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT)  
             .show();
        }
    }
}
一定要注意action的设置,不要与别的服务的action相同,会造成冲突,本人在这就上了当。
详细参考文档  http://blog.csdn.net/lmj623565791/article/details/38461079
```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值