- 新建一个工程 ,包含app(server) 跟client两个module
- 在server端 new -》 aidl 建立Girl.aidl
// Girl.aidl
package so.com.testserver
parcelable Girl
- 建立girl类
package so.com.testserver;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Administrator on 2015/7/17.
*/
public class Girl implements Parcelable {
String name;
int age;
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;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
public static final Creator<Girl> CREATOR=new Creator<Girl>() {
@Override
public Girl createFromParcel(Parcel source) {
Girl girl=new Girl();
girl.setName(source.readString());
girl.setAge(source.readInt());
return girl;
}
@Override
public Girl[] newArray(int size) {
return new Girl[size];
}
};
}
- build 建立halow.aidl
// halow.aidl
package so.com.testserver
import so.com.testserver.Girl
// Declare any non-default types here with import statements
interface halow {
String sayHello()
Girl GetGirl()
}
- build -> rebuild project
- Aidlservice 类
package so.com.testserver;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class Aidlservice extends Service {
public Aidlservice() {
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
halow.Stub binder=new halow.Stub() {
@Override
public String sayHello() throws RemoteException {
return "hellow i am adam";
}
@Override
public Girl GetGirl() throws RemoteException {
Girl girl=new Girl();
girl.setAge(19);
girl.setName("小李欢");
return girl;
}
};
}
- androidManifest.xml 配置 service (intent-filter)
<service
android:name=".Aidlservice"
android:enabled="true"
android:process=":remote"
android:exported="true" >
<intent-filter>
<action android:name="so.com.testserver.Aidlservice"></action>
</intent-filter>
</service>
- 新建一个testclient客户端
9.将server中的 Girl.aidl 跟 halow.aidl拷贝到以及Girl.java拷贝到client module中 ,注意 包名要相同 。
10.测试client MainActivity.java
package so.com.testclient;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import so.com.testserver.Girl;
import so.com.testserver.halow;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
halow halows;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.tv);
button= (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("so.com.testserver.Aidlservice");
bindService(intent,connection,BIND_AUTO_CREATE);
}
});
}
ServiceConnection connection=new ServiceConnection() {
String content;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
halows=halow.Stub.asInterface(service);
try {
content=halows.sayHello()+"/n";
Girl mygirl=halows.GetGirl();
content+=content+mygirl.getName();
textView.setText(content);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
halows=null;
}
};
}