1:直接写一个接口文件PersonInterface.aidl,文件中的方法就是可以提供给别的进程可以访问的方法。保存后会自动在gen目录下生成PersonInterface.java文件
注意:1:此必须.aidl为后缀名 2:必须显示加包和导入要用到的类,那怕是在同一个包下也要引入
直接上代码:
package org.lzm.android.aidl;
import org.lzm.android.aidl.Person;
interface PersonInterface{
void sayHello();
Person getPerson();
}
自动生成的类PersonInterface.java,此类为自动生成,格式有点臭,我没有去动它,哈哈
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: F:\\workspace\\AIDLDemo\\src\\org\\lzm\\android\\aidl\\PersonInterface.aidl
*/
package org.lzm.android.aidl;
public interface PersonInterface extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements org.lzm.android.aidl.PersonInterface
{
private static final java.lang.String DESCRIPTOR = "org.lzm.android.aidl.PersonInterface";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an org.lzm.android.aidl.PersonInterface interface,
* generating a proxy if needed.
*/
public static org.lzm.android.aidl.PersonInterface asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof org.lzm.android.aidl.PersonInterface))) {
return ((org.lzm.android.aidl.PersonInterface)iin);
}
return new org.lzm.android.aidl.PersonInterface.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_sayHello:
{
data.enforceInterface(DESCRIPTOR);
this.sayHello();
reply.writeNoException();
return true;
}
case TRANSACTION_getPerson:
{
data.enforceInterface(DESCRIPTOR);
Person _result = this.getPerson();
reply.writeNoException();
if ((_result!=null)) {
reply.writeInt(1);
_result.writeToParcel(reply, android.os.Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
}
else {
reply.writeInt(0);
}
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements org.lzm.android.aidl.PersonInterface
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void sayHello() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_sayHello, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public Person getPerson() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
Person _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getPerson, _data, _reply, 0);
_reply.readException();
if ((0!=_reply.readInt())) {
_result = Person.CREATOR.createFromParcel(_reply);
}
else {
_result = null;
}
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_sayHello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getPerson = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void sayHello() throws android.os.RemoteException;
public Person getPerson() throws android.os.RemoteException;
}
2:写一个JAVABEAN类且必须实现Parcelable接口
package org.lzm.android.aidl;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
public Person(){
}
public Person(Parcel p){
name = p.readString();
age = p.readInt();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
}
static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}
@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Person(source);
}
};
}
3: 再定义一个Person.aidl文件
内容很少只有一行代码:parcelable Person;但parcelable 要小写
parcelable Person;
4:关键的server类:PersonService.java
此类中就是你要调用前面PersonInterface.aidl中定义好的方法。这样就可以了。
package org.lzm.android.aidl;
import org.lzm.android.aidl.PersonInterface.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class PersonService extends Service {
private static final String TAG = "PersonService";
PersonInterface.Stub mBinder = new Stub() {
@Override
public void sayHello() throws RemoteException {
System.out.println("hello");
}
@Override
public Person getPerson() throws RemoteException {
// TODO Auto-generated method stub
Person p = new Person();
p.setAge(11);
p.setName("lzm");
return p;
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG, "onCreate");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
5:在AndroidManifest.xml中注册你自己实现的Service,此处的intentfilter中的Action要与你在调用bindService方法的第一个参数intent的action一样,如下所示:
<action android:name="org.lzm.android.aidl.PersonInterface"></action> 与bindService(new Intent(""), conn, BIND_AUTO_CREATE);
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lzm.android.aidl"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AIDLDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="PersonService"
android:process=":remote">
<intent-filter>
<action android:name="org.lzm.android.aidl.PersonInterface"></action>
</intent-filter>
</service>
</application>
</manifest>
6.这样就可以了在运行之。
工程目录如下 ,不好意思图没有上传成功!!!
洗洗,睡睡啦!!