Android 8,android基础知识总结

该博客详细介绍了Android中使用AIDL进行远程服务(RemoteService)和本地服务(LocalService)之间的交互。在onCreate()方法中启动绑定过程,通过onBind()返回服务的Binder接口。当服务连接成功时,执行bindSuccess()方法,而在unbind()方法中解除绑定。同时,当服务断开连接时,会启动一个新的TransferActivity。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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”));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值