Service端------------------------------------------------------------------------------------------------------
1、创建A.aidl文件,里面定义个接口A(名字必须和文件名一样、并且需要手动导入该文件所在的包,
eg:package com.xy.service;),接口里写自己要的方法,这里我写了String sayHello();文件保存
之后系统会自动在gen目录下生成接口名A对应的Java文件,里面一大推东西不需要要看懂。
如下:
package com.xy.service;
interface A{
String getName();//返回String
Boy getBoy();//返回Boy对象,该对象是有要求的
List<Boy> getAllBoy();//返回集合
}
这样写的话还需要其它类,一个实现了Parcelable接口的Boy类(属于自定义的类因此需要手动
import Boy所在包,eg:import com.xy.service.Boy;<用到boy类的aidl文件都需要手动import>),还需要Boy.aidl,内容如下
package com.xy.service;//同样需要手动package
parcelable Boy;//这里很简单的一句话,注意大小写
//Boy.java...........
package com.xy.service;
import android.os.Parcel;
import android.os.Parcelable;
public class Boy implements Parcelable {
//这个是必须写的
public static final Parcelable.Creator<Boy> CREATOR = new Parcelable.Creator<Boy>() {
public Boy createFromParcel(Parcel in) {
return new Boy(in);//用自己写的类型就行
}
public Boy[] newArray(int size) {
return new Boy[size];//用自己写的类型就行
}
};
//无参构造方法
public Boy() {
}
//必须的构造方法
private Boy(Parcel in) {
name = in.readString();
age = in.readInt();
}
//方便测试的构造方法
public Boy(String name, int age) {
super();
this.name = name;
this.age = age;
}
private String name;
private 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;//一般是返回0就行
}
//实现接口的方法
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);//参数是前面定义的字段
dest.writeInt(age);//参数是前面定义的字段
}
}
2、创建一个Service,需要继承系统的Service。关键是创建一个AIDL对象并返回,在里面实现之前定义的方法。
如下:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return myBind;
}
private A.Stub myBind = new A.Stub() {
@Override
public String getName() throws RemoteException {
return "我是小袁";
}
@Override
public Boy getBoy() throws RemoteException {
return new Boy("张三",35);
}
@Override
public List<Boy> getAllBoy() throws RemoteException {
List<Boy> list = new ArrayList<Boy>();
list.add(new Boy("李四", 10));
list.add(new Boy("王五", 15));
list.add(new Boy("无极", 21));
list.add(new Boy("三丰", 88));
return list;
}
};
}
3、在AndroidManifest.xml文件中对Service进行配置,如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xy.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ServiceActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<--添加的对Service的配置在这里!-->
<!-- android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果
是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程 -->
<service android:name=".MyService" android:process=":remote">
<intent-filter >
<action android:name="com.xy.service.MyService"/>
</intent-filter>
</service>
</application>
</manifest>
Client端------------------------------------------------------------------------------------------------------
1、将Service端的两个AIDL文件(如果有自定义的类同样需要拿过去,eg:Boy.class)Copy过来放在该工程里和之前该文件相同的目录下(可能需要新建一个包),保存之后同样会
在gen目录下自动生成Java文件
2、在main.xml里添加个Button用来测试该程序
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="调用AIDl"/>
</LinearLayout>
3、在主Activity里找到这两个控件并监听Button的事件,主要是创建一个ServiceConnection
public class CilentActivity extends Activity {
private TextView tv;//用来显示结果的
private ServiceConnection mConnection = new ServiceConnection() {
private A a = null;//AIDL对象
public void onServiceDisconnected(ComponentName name) {
a = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
a = A.Stub.asInterface(service);
// aidl通信
try {
String mText = "Say hello: " + a.getName() + "/n";
boolean bool = a.getAllBoy() == null ? false : true;
tv.setText("getName is : " + mText + "getBoy's name is "
+ a.getBoy().getName() + "getBoy's age is "
+ a.getBoy().getAge() + " /n bool is " + bool);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
private void initView() {
tv = (TextView) findViewById(R.id.textView);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//这里启动的就是之前配置的服务名
Intent intent = new Intent("com.xy.service.MyService");
bindService(intent, mConnection,BIND_AUTO_CREATE);
}
});
}
}
android AIDL简单步骤
最新推荐文章于 2024-03-27 18:44:44 发布