对于AIDL,在官方文档中不好理解。
这里写出建立AIDL工程的过程。
/*这里需要提一下,很多时候,我们可以通过android序列化将对象在进程间传递,这是一种不错的选择,实现很简单,只要让一个类实现pacelable接口就OK,其传递和普通类型的传递一样*/
首先写服务端
1.建立AIDL文件。在androids studio中建AIDL文件要注意的地方-->http://blog.chinaunix.net/uid-29532371-id-4905575.html
RemoteServiceAidl.java
-
// RomoteServiceAidl.aidl
-
package com.vibexie.myaidl;
-
-
// Declare any non-default types here with import statements
-
-
interface RemoteServiceAidl {
-
/**
-
*在Android Studio中写AIDL文件,必须没次修改AIDL文件后,要重新Make Project,make还是不行的话,重新打开工程即可
-
*对于java基本类型和String类型,默认是in,可以注明in,out,还是inout,但List,Map等类型必须指定,否者报错
-
*这里举getList这个方法中,Client调用getList(),传入list,list2,其中list已经赋值,在远程Service中,将list的值赋给list2,
-
*并且在Client的getList中直接更新了list2,这就是in和out的作用
-
*/
-
String getData(String name);
-
void getList(in List<String> list,out List<String> list2);
- }
RemoteService.java
-
package com.vibexie.myaidl;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Binder;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.widget.Toast;
-
-
import java.util.List;
-
-
public class RemoteService extends Service {
-
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
Toast.makeText(getApplicationContext(),"启动Service",Toast.LENGTH_SHORT).show();
-
}
-
-
/**
-
* new一个binder实现AIDL接口,并将binder通过onBind()返回给service
-
*/
-
Binder binder=new RemoteServiceAidl.Stub() {
-
@Override
-
public String getData(String name) throws RemoteException {
-
return name+"+1";
-
}
-
-
@Override
-
public void getList(List<String> list,List<String> list2) throws RemoteException {
-
/**
-
* 这里将list的第一个String赋给list2的第一个String,这就是service的服务功能
-
*/
-
list2.add(list.get(0));
-
}
-
};
-
-
@Override
-
public IBinder onBind(Intent intent) {
-
// TODO: Return the communication channel to the service.
-
return binder;
-
}
- }
3.在ActivityMainfest.xml注册服务,并且过滤AIDL
点击(此处)折叠或打开
-
<service
-
android:name=".RemoteService"
-
android:enabled="true"
-
android:exported="true" >
-
<intent-filter>
-
<action android:name="com.vibexie.myaidl.RemoteServiceAidl"></action>
- </intent-filter>
- </service>
4.在Activity中启动Service
ActivityMain.java
-
package com.vibexie.myaidl;
-
-
import android.content.Intent;
-
import android.support.v7.app.ActionBarActivity;
-
import android.os.Bundle;
-
import android.view.Menu;
-
import android.view.MenuItem;
-
import android.view.View;
-
import android.widget.Button;
-
-
-
public class MainActivity extends ActionBarActivity {
-
private Button button;
-
private Intent intent;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
button=(Button)this.findViewById(R.id.button);
-
button.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
intent=new Intent(MainActivity.this,RemoteService.class);
-
startService(intent);
-
}
-
});
-
}
-
-
/**
-
* 记得关闭service,否者service不会stop
-
*/
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
stopService(intent);
-
}
- }
接下来就是客户端Client了
1.定义与服务端一模一样的AIDL文件,注意包名必须和服务端AIDL所在的包名一致,否则报错
2.建立Activity,调用远程服务
MainActivity.java
-
package com.vibexie.myaidl;
-
-
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.ActionBarActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
-
public class MainActivity extends ActionBarActivity {
-
private Button button;
-
private Button button2;
-
private RemoteServiceAidl remoteServiceAidl;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
button=(Button)this.findViewById(R.id.button);
-
button2=(Button)this.findViewById(R.id.button2);
-
-
button.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
/**
-
* 绑定远程Service
-
*/
-
Intent intent=new Intent(RemoteServiceAidl.class.getName());
-
bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
-
Toast.makeText(MainActivity.this,"绑定service",Toast.LENGTH_SHORT).show();
-
}
-
});
-
-
button2.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
try {
-
//Toast.makeText(MainActivity.this,""+remoteServiceAidl.getData("vibexie"),Toast.LENGTH_SHORT).show();
-
List<String> list=new ArrayList<String>();
-
List<String> list2=new ArrayList<String>();
-
list.add("vibexie");
-
/**
-
* 调用远程service接口方法,请求服务
-
*/
-
remoteServiceAidl.getList(list,list2);
-
Toast.makeText(MainActivity.this,""+"list2被赋值"+list2.get(0),Toast.LENGTH_SHORT).show();
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
});
-
}
-
-
private ServiceConnection serviceConnection=new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
remoteServiceAidl=RemoteServiceAidl.Stub.asInterface(service);
-
}
-
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
}
-
};
- }
服务端:

客户端: