进程间通讯 AIDL实际使用步骤
2019-03-15 本文已影响0人
next_discover
1、编写aidl文件
Book.aidl
// Book.aidl
package test.test;
// Declare any non-default types here with import statements
parcelable Book;
IMyAidlInterface.aidl
注意导入import
// IMyAidlInterface.aidl
package test.test;
import test.test.Book;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
List<Book> getBookList();
void addBook(inout Book book);
}
2、编写Service服务
AIDLService.java
package test.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class AIDLService extends Service {
private ArrayList<Book> bookList;
@Override
public void onCreate() {
super.onCreate();
bookList = new ArrayList<Book>();
bookList.add(new Book("语文"));
bookList.add(new Book("数学"));
bookList.add(new Book("英语"));
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return stub;
}
private final IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub(){
@Override
public List<Book> getBookList() throws RemoteException {
return bookList;
}
@Override
public void addBook(Book book) throws RemoteException {
bookList.add(book);
}
};
}
3、绑定服务
MainActivity.java
bindService绑定服务
public class MainActivity extends AppCompatActivity {
@BindView(R.id.button)
Button mButton;
@BindView(R.id.frame_layout)
FrameLayout mFrameLayout;
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//首先要绑定服务
bindService(new Intent(this,AIDLService.class),serviceConnection, Service.BIND_AUTO_CREATE);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
iMyAidlInterface.addBook(new Book("体育"));
List<Book> bookList = iMyAidlInterface.getBookList();
for (Book book:bookList){
Log.e("book",book.getName());
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface = null;
Toast.makeText(MainActivity.this, name.toString(), Toast.LENGTH_SHORT).show();
}
};
}
4、配置不同的进程
AndroidManifest.xml
android:process配置不同的进程
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".AIDLService"
android:process=":aidl"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>