服务端:
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
```