IPC也看过很多次,每次看完后记会忘记,这次自己给它写下来加深印象
IPC
进程间通信(Inter-Process Communication),简称IPC,就是指进程与进程之间进行通信
基于Binder的AIDL和Messager的同一个应用的通信
步骤一
首先写个最基本的就是重写IBinder,增强服务端的的功能 看代码:
//IpcService.java文件
public class IpcService extends Service {
private IBinder mIBinder;
@Override
public void onCreate() {
super.onCreate();
mIBinder = new MyBinder();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mIBinder;
}
public class MyBinder extends Binder {
public void print() {
Log.d("PHN", "服务Service,执行任务");
}
}
}
注意要在清单文件中注册服务
public class MainActivity extends AppCompatActivity {
private IpcService.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自动绑定服务
Intent intent = new Intent(this, IpcService.class);
MyServiceConnection serviceConnection = new MyServiceConnection();
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != myBinder) {
myBinder.print();
}
}
});
}
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (IpcService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
loginfo_01.png
执行点击服务端会打印信息,说明这个活动和服务进行通信是成功的!
步骤二
我们用Messager来做刚才的功能
1.首先在Service里面创建一个Hander用来接受消息:
private final static Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("PHN", "服务Service,执行任务");
}
};
2.在Service里面创建一个Messager,并把Handler放入其中
private final static Messenger mMessenger = new Messenger(mHandler);
3.重写onbind方法,返回Messager里面的Binder
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
贴出完整的服务代码
public class IpcService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
private final static Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("PHN", "服务Service,执行任务");
}
};
private final static Messenger mMessenger = new Messenger(mHandler);
}
再贴出活动里的代码
public class MainActivity extends AppCompatActivity {
private Messenger messenger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自动绑定服务
Intent intent = new Intent(this, IpcService.class);
MyServiceConnection serviceConnection = new MyServiceConnection();
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != messenger) {
Message message = Message.obtain();
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
messenger = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
执行看看结果
loginfo_02.png果然可以得到同样的效果!
步骤三
上面我们可能看到只能是活动让服务执行任务,那么怎样才能让服务端让活动也执行任务呢,我们先对服务器端的代码进行修改,首先修改Service的Handler
看下面服务端代码处理:
public class IpcService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
private final static Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("PHN", "服务Service,执行任务");
//获取Messager
Messenger messenger = msg.replyTo;
//创建消息
Message msg_reply = Message.obtain();
try {
//发送
messenger.send(msg_reply);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
private final static Messenger mMessenger = new Messenger(mHandler);
}
接着我们在活动端也增加一个Handler和Messager来处理消息,贴出代码
public class MainActivity extends AppCompatActivity {
private Messenger messenger;
private final static Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("PHN", "活动Acitivity,执行任务");
}
};
private final static Messenger mReplyMessager = new Messenger(mHandler);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自动绑定服务
Intent intent = new Intent(this, IpcService.class);
MyServiceConnection serviceConnection = new MyServiceConnection();
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != messenger) {
Message message = Message.obtain();
//通过msg把客户端的Messager传送到服务器端(关键代码)
message.replyTo =mReplyMessager;
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
}
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
messenger = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
还有一个比较关键的地方,就是要在客户端发送消息的时候把客户端的Messager通过消息传送到服务器端
(msg.replyTo =mReplyMessager)
来执行看看结果:
loginfo_03.png果然可以!!
步骤四
上面的通信是基于本应用里的,下面我们来看看不同的两个应用之间的通信AIDL,我是参照任玉刚的开发艺术里写例子,
1.首先那就是新建AIDL文件,很简单:
那我就直接创建两个aidl文件Book.aidl,BookManager.aidl
screenshot_02.png下面直接看文件的具体内容,我贴上来:
//Book.java
public class Book implements Parcelable{
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];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
private String name;
private int price;
public Book() {}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(price);
}
/**
* 参数是一个Parcel,用它来存储与传输数据
* @param dest
*/
public void readFromParcel(Parcel dest) {
//注意,此处的读值顺序应当是和writeToParcel()方法中一致的
name = dest.readString();
price = dest.readInt();
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
// Booka.aidl
package com.hanny.aidldemo;
// Declare any non-default types here with import statements
parcelable Book;
// BookManager.aidl
package com.hanny.aidldemo;
import com.hanny.aidldemo.Book;
// Declare any non-default types here with import statements
interface BookManager {
//所有的返回值前都不需要加任何东西,不管是什么数据类型
List getBooks();
//传参时除了Java基本类型以及String,CharSequence之外的类型
//都需要在前面加上定向tag,具体加什么量需而定
void addBook(in Book book);
}
文件搞好了,先编译下,看看会不会错,应该会错。
loginfo_03.png这是因为把Book.java文件放到了aidl下,所以要在build.gradle 的android{ }的闭包下加入
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/main/aidl']
}
}
不会错了,那就来写AIDL的service文件:
public class AIDLService extends Service {
//包含Book对象的list
private List mBooks = new ArrayList<>();
private BookManager.Stub bookManager = new BookManager.Stub() {
@Override
public List getBooks() throws RemoteException {
if (mBooks != null) {
return mBooks;
}
return new ArrayList();
}
@Override
public void addBook(Book book) throws RemoteException {
synchronized (this) {
if (mBooks == null) {
mBooks = new ArrayList();
}
if (book == null) {
book = new Book();
}
book.setPrice(23);
if (!mBooks.contains(book)) {
mBooks.add(book);
}
//打印mBooks列表,观察客户端传过来的值
Log.e("PHN", "增加后的图书 : " + mBooks.toString());
}
}
};
@Override
public void onCreate() {
super.onCreate();
Book book = new Book();
book.setName("Android开发艺术探索");
book.setPrice(28);
mBooks.add(book);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return bookManager;
}
}
服务端写好了,那就新建一个工程,然后把服务端的aidl下的文件全部拷贝过来~放在main下级目录!
screenshot_03.png接着在活动里用隐式意图开启服务!这样的话就要在服务端的清单文件里给服务配上action
<service android:name=".AIDLService">
<intent-filter>
<action android:name="action.ipc"/>
</intent-filter>
</service>
看看客户端活动里的代码:
public class MainActivity extends AppCompatActivity {
//标志当前与服务端连接状况的布尔值,false为未连接,true为连接中
private boolean mBound = false;
private BookManager bookManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_get).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addBook();
}
});
}
private void addBook() {
if (!mBound) {
attemptToBindService();
Toast.makeText(this, "当前与服务端处于未连接状态,正在尝试重连,请稍后再试", Toast.LENGTH_SHORT).show();
return;
}
if (bookManager == null) return;
Book book = new Book();
book.setName("APP研发录In");
book.setPrice(30);
try {
bookManager.addBook(book);
Log.e(getLocalClassName(), book.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
//开启服务
private void attemptToBindService() {
Intent intent = new Intent();
intent.setAction("action.ipc");
intent.setPackage("com.hanny.aidldemo");
bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
bookManager = BookManager.Stub.asInterface(service);
mBound = true;
if (bookManager != null) {
try {
List books = bookManager.getBooks();
Log.d("PHN", "初始化得到的书: "+books.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mServiceConnection);
mBound = false;
}
}
}
下面先运行服务端apk ,然后运行客服务apk然后点击增加图书,看看客户端和服务端的打印结果:
Paste_Image.png反正就是这么简单,动手实践加深印象,并没有什么难的地方,
注意地方
AIDL 发生异常的原因 Android java.lang.SecurityException: Binder invocation to an incorrect interface
解决方法如下:
在使用上请注意,服务端与客户端都要有相同的接口(使用到的),这里的“相同”是指完全相同,包括包名,也就是说要在不同工程下建立相同的包名,这样一来,问题应该迎刃而解了!
网友评论