1.AIDL跨进程通信,先定义两个进程app1,app2
1.app1里 先定义一个service 在AndroidManifest.xml里注册 如下:
<service
android:name=".MyService"
android:exported="true">
<intent-filter>
<action android:name="com.lypeer.aidl" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mIMyAidlInterface;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private IMyAidlInterface.Stub mIMyAidlInterface = new IMyAidlInterface.Stub() {
@Override
public String getName() throws RemoteException {
return "test";
}
@Override
public User getUser() throws RemoteException {
return new User("xuelei");
}
};
}
2.app1里继续 在main下面创建个aidl文件夹,aidl下面创建个和包名一样的文件夹 ,里面放aidl格式的文件,
package com.example.lenovo.myapplication;
// Declare any non-default types here with import statements
import com.example.lenovo.myapplication.User;
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
String getName();
User getUser();
}
//这是第二个文件
package com.example.lenovo.myapplication;
parcelable User;
3.app1里创建java类user
public class User implements Parcelable {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
}
protected User(Parcel in) {
this.name = in.readString();
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
}
4.App2里 ,把app1的aidl的文件夹全部拷贝过来 包括app1的包名 要一模一样,在app2的java文件夹下新建app1包名的目录,里面放数据类user类,也是从app1里直接拷贝过来
5.app2里 直接用bindService调用 通过intent隐式
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface mIMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("com.lypeer.aidl");
intent.setPackage("com.example.lenovo.myapplication");
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}, BIND_AUTO_CREATE);
findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
if (mIMyAidlInterface != null) {
User user = mIMyAidlInterface.getUser();
Toast.makeText(MainActivity.this, user.getName(), Toast.LENGTH_SHORT).show();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
6.代码目录格式如图
微信图片_20190812175250.png
网友评论