前言
上次学习AIDL还是Eclipse+ADT的年代,平常也不怎么用,好记性不如烂笔头,现记录下具体做法。
AIDL:Android interface Definition Language,用于实现进程间通信
感谢:https://www.cnblogs.com/zhujiabin/p/6080806.html
正文
要实现的功能是:在app AIDLClient中,修改app AIDLServer中Book的价格,并返回Book对象。
app AIDLServer目录结构:
image.png实体类Book
package com.app.aidlservice.aidlservice.bean;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
private String name;
private int price;
public Book() {
}
public Book(String name, int price) {
this.name = name;
this.price = price;
}
//setter and getter......
//......
protected Book(Parcel in) {
name = in.readString();
price = in.readInt();
}
public static final Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(price);
}
public void readFromParcel(Parcel in) {
name = in.readString();
price = in.readInt();
}
}
在main目录上new AIDL文件,如图
image.png会生成自动生成与java目录平级的aidl目录,将IMyAidlInterface中的方法删掉,换成自己的方法。
注意导入Book类: 在aidl下面的 com.app.aidlservice.aidlservice包中新建/bean/Book.aidl。
// Book.aidl
package com.app.aidlservice.aidlservice.bean;
parcelable Book;
package com.app.aidlservice.aidlservice;
import com.app.aidlservice.aidlservice.bean.Book;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
Book getBook();
void setPrice(int price);
}
BookService:
package com.app.aidlservice.aidlservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.app.aidlservice.aidlservice.bean.Book;
public class BookService extends Service {
Book book;
@Override
public void onCreate() {
super.onCreate();
book = new Book("java 并发编程的艺术", 59);
}
@Override
public IBinder onBind(Intent intent) {
return new IMyAidlInterface.Stub() {
@Override
public Book getBook() throws RemoteException {
return book;
}
@Override
public void setPrice(int price) throws RemoteException {
book.setPrice(price);
}
};
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
在清单文件中配置Service
<service
android:name=".BookService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.app.aidlservice.BookService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
app AIDLClient
image.png将app AIDLService中的aidl包全部拷贝过来,涉及到的Book类也要放到对应的包名下。
在activity中来绑定服务。
package com.app.aidlservice.aidlclient;
import android.content.ComponentName;
import android.content.Context;
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.util.Log;
import android.view.View;
import com.app.aidlservice.aidlservice.IMyAidlInterface;
import com.app.aidlservice.aidlservice.bean.Book;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_bind).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.app.aidlservice.BookService");
intent.setPackage("com.app.aidlservice.aidlservice");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
});
findViewById(R.id.btn_set_price).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (iMyAidlInterface != null) {
try {
iMyAidlInterface.setPrice(100);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
findViewById(R.id.btn_get_book).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (iMyAidlInterface != null) {
try {
Book book = iMyAidlInterface.getBook();
Log.e("TAG", "Book Name: " + book.getName() + ", Book Price: " + book.getPrice());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}
private IMyAidlInterface iMyAidlInterface;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
打印结果:
07-10 23:54:11.103 3468-3468/com.app.aidlservice.aidlclient E/TAG: Book Name: java 并发编程的艺术, Book Price: 100
可以看到,价格由之前的59,变为100。
至此,使用aidl实现了进程间通信。
网友评论