此种服务的好处在于,多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作,下面将通过一个DEMO 演示AIDL 是如何为应用程序之间提供服务的。
本文大纲为:
- 1、创建AIDL 服务端。
- 2、创建AIDL 客户端。
- 3、客户端调用服务端提供的服务接口。
- 4、小结。
本文要实现的功能大致如下:创建AIDL服务端,此服务端将提供一个Person的javabean 提供客户端取得数据,因为aidl 支持的数据类型比较简单,故这里建议把常用的数据类型的数据写入服务
package com.example.server;
import com.example.server.IPerson;
import com.example.server.R;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button bindButton;
private Button unbindButton;
private IPerson iPerson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindButton = (Button)findViewById(R.id.button1);
unbindButton = (Button)findViewById(R.id.button2);
bindButton.setOnClickListener(this);
unbindButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(MainActivity.this,MyService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.button2:
unbindService(conn);
break;
default:
break;
}
}
private ServiceConnection conn = new ServiceConnection() {
//连接对象
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
iPerson = IPerson.Stub.asInterface(service);
if(iPerson!=null){
try {
iPerson.setValue("AIDL TEST");
Toast.makeText(MainActivity.this, "赋值成功", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "赋值失败", Toast.LENGTH_LONG).show();
}
}
}
};
}
package com.example.server;
import com.example.server.IPerson;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service{
private IPerson.Stub iPerson = new Person();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iPerson;
}
}
package com.example.server;
import com.example.server.IPerson;
import android.os.RemoteException;
public class Person extends IPerson.Stub{
private String name;
@Override
public void setValue(String name) throws RemoteException {
this.name = name;
}
@Override
public String getValue() throws RemoteException {
return this.name;
}
}
//aidl
package com.example.server;
interface IPerson{
void setValue(String name);
String getValue();
}
2、创建AIDL 客户端
package com.example.client;
import com.example.server.IPerson;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
private IPerson person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.example.server.MyService");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public synchronized void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
person = IPerson.Stub.asInterface(service);
if(person != null){
try {
String name1 = person.getValue();
Toast.makeText(MainActivity.this, "远程调用成功,值为:"+name1, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "远程调用失败", Toast.LENGTH_LONG).show();
}
}
}
};
//aidl
package com.example.server;
interface IPerson{
void setValue(String name);
String getValue();
}
}