上图为所建工程结构 因为android AIDL建立时需要包名相同 ,所以建议copy,注意运行时先运行一次service,不要犯低级错误哈
1:服务端和客户端都有的相同的aidl文件,自动生成文件就不贴了
package com.pengxiaolog.aidl;
interface ServiceAidl{
void setName(String name);
String getName();
}
2:服务端Service 类
package com.pengxiaolog.aidl;
import com.pengxiaolog.aidl.ServiceAidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service{
private String Myname;
class MyBind extends ServiceAidl.Stub{
@Override
public void setName(String name) throws RemoteException {
// TODO Auto-generated method stub
Myname=name;
Log.e("================>>you [set] my name is", Myname);
}
@Override
public String getName() throws RemoteException {
// TODO Auto-generated method stub
Log.e("================>>you [get] my name is", Myname);
return Myname;
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new MyBind();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
下面贴出服务端以及客户端的测试Activity 注意两者的调用区别
Service工程activity类:
package com.pengxiaolog.aidl;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.androidaidlservce.R;
public class MainActivitty extends Activity implements OnClickListener{
private ServiceAidl myService;
private Button btn[];
private int id[]=new int[]{R.id.button1,R.id.button2,R.id.button3,R.id.button4};
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
myService=null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myService=ServiceAidl.Stub.asInterface(arg1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewInit();
}
private void viewInit(){
btn=new Button[4];
for (int i = 0; i < btn.length; i++) {
btn[i]=(Button) this.findViewById(id[i]);
btn[i].setOnClickListener(this);
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.button1:
bindService(new Intent("COM.PENGXIAOLONG.STYLE"), serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.button2:
unbindService(serviceConnection);
break;
case R.id.button3:
try {
if(myService!=null)
myService.setName("NAME");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.button4:
try {
if(myService!=null)
myService.getName();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}
client 端activity类:
package com.pengxiaolog.aidl;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.androidaidlclient.R;
public class Mainactivity extends Activity implements OnClickListener{
private ServiceAidl myService;
private Button btn[];
private int id[]=new int[]{R.id.button1,R.id.button2,R.id.button3,R.id.button4};
ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
myService=null;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
myService=ServiceAidl.Stub.asInterface(arg1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewInit();
}
private void viewInit(){
btn=new Button[4];
for (int i = 0; i < btn.length; i++) {
btn[i]=(Button) this.findViewById(id[i]);
btn[i].setOnClickListener(this);
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.button1:
bindService(new Intent("COM.PENGXIAOLONG.STYLE"), serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.button2:
unbindService(serviceConnection);
break;
case R.id.button3:
try {
if(myService!=null)
myService.setName("NAME");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.button4:
try {
if(myService!=null)
myService.getName();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
最后别忘了服务要在主配置配置一下
<service
android:name="com.pengxiaolog.aidl.MyService"
android:exported="true" >
<intent-filter>
<action android:name="COM.PENGXIAOLONG.STYLE" />
</intent-filter>
</service>