AIDL:Android Interface Definition Language,即Android接口定义语言。
Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。
做一个客户端和服务起的demo
执行步骤:
service:
1, 在service中新建一个.aidl的文件IQuoteService. gen下会自动生成一个.java文件
package com.fuyun.quoteservice;
interface IQuoteService{
String getName();
}
2.在service 中新建一个继承自Server的类QuoteService,重写OnBind().返回.aidl实例
3.在QuoteService中定义一个内部类QuoteServiceImpl.用于实现IQuoteService接口
package com.fuyun.quoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class QuoteService extends Service{
public class IQuoteServiceImpl extends IQuoteService.Stub{
@Override
public String getName() throws RemoteException {
// TODO Auto-generated method stub
return "Kevin Dan";
}
}
@Override
public IBinder onBind(Intent intent) {
return new IQuoteServiceImpl();
}
}
4.在Manifest中注册server. (注意:service的name和action的那么不一样)
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="QuoteService">
<intent-filter >
<action android:name="com.fuyun.quoteservice.IQuoteService"/>
</intent-filter>
</service>
</application>
client:
1.新建一个包,包名与service中的.aidl文件的所在的包名相同,把Service中的.aidl文件复制到该包下
2.新建一个MainActivity.用于连接Service。访问服务器
package com.fuyun.myquoteclient;
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.TextView;
import android.widget.Toast;
import com.fuyun.quoteservice.IQuoteService;
public class MainActivity extends Activity implements OnClickListener{
private IQuoteService mQuoteService;
private TextView mBindBtn;
private TextView mGetMsgBtn;
private TextView mUnBindBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBindBtn = (TextView)findViewById(R.id.bind);
mGetMsgBtn = (TextView)findViewById(R.id.get_msg);
mUnBindBtn = (TextView)findViewById(R.id.un_bind);
mBindBtn.setOnClickListener(this);
mGetMsgBtn.setOnClickListener(this);
mUnBindBtn.setOnClickListener(this);
mGetMsgBtn.setEnabled(false);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.bind){
bindService(new Intent(IQuoteService.class.getName()), mConn, Context.BIND_AUTO_CREATE);
mBindBtn.setEnabled(false);
mUnBindBtn.setEnabled(true);
mGetMsgBtn.setEnabled(true);
}else if (v.getId() == R.id.get_msg){
dspQuoteMsg();
}else if (v.getId() == R.id.un_bind){
unbindService(mConn);
mBindBtn.setEnabled(true);
mUnBindBtn.setEnabled(false);
mGetMsgBtn.setEnabled(false);
}
}
private void dspQuoteMsg(){
try {
Toast.makeText(this, mQuoteService.getName(), Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private ServiceConnection mConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mQuoteService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mQuoteService = IQuoteService.Stub.asInterface(service);
dspQuoteMsg();
}
};
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
unbindService(mConn);
}
}
例子:http://download.csdn.net/detail/u011743618/6397913