调用远程服务方法步骤
远程绑定服务时无法通过同一个接口来调用方法,这时就需要使用AIDL技术
将接口扩展名改为“.aidl”
去掉权限修饰符
gen文件夹下会生成同名接口
将服务中自定义的IBinder类改为继承接口中的Stub
用Stub.asInterface()方法把ServiceConnection中返回的IBinder转为接口类型
AS中aidl的使用
1.在main下面新建文件夹aidl
2.包名与在java中保持一致
下面是具体实现步骤
aidl接口文件如下,注意自定义的对象参数前要加in标识符,必须显式导入Book,不然as会报错
package mytest.jiang.wei.ipc.aidl;
import java.util.List;
import mytest.jiang.wei.ipc.aidl.Book;
/**
* Created by wei.jiang on 2015/11/24.
*/
interface IBookManager {
List<Book> getBookList();
void addBook(in Book book);
}
Book实体类继承自Parceable,写法这里就不给出了,,当然As可以为我们自动 生成。下面要声明book的aidl文件,如下
package mytest.jiang.wei.ipc.aidl;
parcelable Book;
下面就是Service了,这里在一个程序中,为Service执行android:procee=”:remote”,让Service运行在单独的进程中,要注意对象读写的同步操作,这里使用CopyOnWriteArrayList
public class BookManagerService extends Service {
//内部实现了读写同步
private CopyOnWriteArrayList<Book> bookList = new CopyOnWriteArrayList<Book>();
private List<IOnNewBookArrivedListener> mListenerList = new ArrayList<IOnNewBookArrivedListener>();
private Binder mBinder = new IBookManager.Stub() {
@Override
public List<Book> getBookList() throws RemoteException {
return bookList;
}
@Override
public void addBook(Book book) throws RemoteException {
bookList.add(book);
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
bookList.add(new Book(1, "第一本书"));
bookList.add(new Book(2, "第二本书"));
super.onCreate();
}
}
最后是客户端Activity,两个事件中分别去调addBook与getBookList方法
public class BooKManagerActivity extends Activity {
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
bookManager = IBookManager.Stub.asInterface(service);
try {
List<Book> bookList = bookManager.getBookList();
for (Book book : bookList) {
System.out.println(book.getBookName());
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
private IBookManager bookManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl);
}
public void bind(View v) {
Intent intent = new Intent(this, BookManagerService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void addBook(View v) {
try {
bookManager.addBook(new Book(12, "新书"));
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void getBook(View v) {
try {
List<Book> bookList = bookManager.getBookList();
for (Book book : bookList) {
System.out.println(book.getBookName());
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
上面的都很简单,下面要为addbook添加监听,,这里设计到接口的传递,aidl可以传递aidl本身,所以接口也必须是一个aidl文件。
package mytest.jiang.wei.ipc.aidl;
import mytest.jiang.wei.ipc.aidl.Book;
/**
* Created by wei.jiang on 2015/11/27.
*/
interface IOnNewBookArrivedListener {
void onNewBookArrived(in Book book);
}
然后需要给IBookManager添加两个注册与反注册的方法
interface IBookManager {
List<Book> getBookList();
void addBook(in Book book);
void registerListener(IOnNewBookArrivedListener listener);
void unregisterListener(IOnNewBookArrivedListener listener);
}
这里是典型的观察者模式,Service中需要在addBook中添加回调
private RemoteCallbackList<IOnNewBookArrivedListener> mListenerList = new RemoteCallbackList<IOnNewBookArrivedListener>();
private Binder mBinder = new IBookManager.Stub() {
@Override
public List<Book> getBookList() throws RemoteException {
return bookList;
}
@Override
public void addBook(Book book) throws RemoteException {
bookList.add(book);
notifyListener(book);
}
@Override
public void registerListener(IOnNewBookArrivedListener listener) throws RemoteException {
mListenerList.register(listener);
}
@Override
public void unregisterListener(IOnNewBookArrivedListener listener) throws RemoteException {
mListenerList.unregister(listener);
}
};
public void notifyListener(Book book) throws RemoteException {
System.out.println(book.getBookName());
int N = mListenerList.beginBroadcast();
for (int i = 0; i < N; i++) {
IOnNewBookArrivedListener listener = mListenerList.getBroadcastItem(i);
if (listener != null)
listener.onNewBookArrived(book);
}
mListenerList.finishBroadcast();
}
public void notifyListener(Book book) throws RemoteException {
System.out.println(book.getBookName());
int N = mListenerList.beginBroadcast();
for (int i = 0; i < N; i++) {
IOnNewBookArrivedListener listener = mListenerList.getBroadcastItem(i);
if (listener != null)
listener.onNewBookArrived(book);
}
mListenerList.finishBroadcast();
}
这里要注意的是我们用RemoteCallbackList集合去做存listener,因为客户端设置的listener与服务端的 并不是同一个对象,只是有相同的Binder,所以普通的List并不能实现添加与删除。 RemoteCallbackList通过键值对的形式去查找相应的listener,并实现增减.
接下来就是去Activity注册listener并实现回调函数, 在onServiceConnected中添加如下
bookManager.registerListener(new IOnNewBookArrivedListener.Stub() {
@Override
public void onNewBookArrived(Book book) throws RemoteException {
System.out.println("监听到新书来了:" + book.getBookName());
}
});
这样book的监听就实现了。