public class RemoteService extends Service {
private static final String TAG = “RemoteService”;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, “onCreate: 创建 RemoteService”);
bindLocalService();
}
@Override
public IBinder onBind(Intent intent) {
return stub;
}
private IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() {
@Override
public void bindSuccess() throws RemoteException {
Log.e(TAG, “bindSuccess: LocalService 绑定 RemoteService 成功”);
}
@Override
public void unbind() throws RemoteException {
Log.e(TAG, “unbind: 此处解除 RemoteService 与 LocalService 的绑定”);
getApplicationContext().unbindService(connection);
}
};
/**
- 绑定 LocalService
*/
private void bindLocalService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName(“com.wuzy.aidlclient”, “com.wuzy.aidlclient.LocalService”));
if (!getApplicationContext().bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
Log.e(TAG, “bindLocalService: 绑定 LocalService 失败”);
stopSelf();
}
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
// bindRemoteService();
createTransferActivity();
}
};
private void createTransferActivity() {
Intent intent = new Intent(this, TransferActivity.class);
intent.setAction(TransferActivity.ACTION_FROM_SELF);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
public class LocalService extends Service {
private static final String TAG = “LocalService”;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, “onCreate: 创建 LocalService”);
bindRemoteService();
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, “onBind: 绑定 LocalService”);
return stub;
}
private IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() {
@Override
public void bindSuccess() throws RemoteException {
Log.e(TAG, “bindSuccess: RemoteService 绑定 LocalService 成功”);
}
@Override
public void unbind() throws RemoteException {
getApplicationContext().unbindService(connection);
}
};
private ServiceConnection connection = new ServiceConnection() {
@Overri
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
de
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
// bindRemoteService();
createTransferActivity();
}
};
private void createTransferActivity() {
Intent intent = new Intent(this, TransferActivity.class);
intent.setAction(TransferActivity.ACTION_FROM_SELF);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void bindRemoteService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName(“com.wuzy.aidlserver”, “com.wuzy.aidlserver.RemoteService”));