fix: 重新打开 Activity 时恢复串流状态
Activity 被销毁后重新打开时,若后台 ScreenCaptureService 仍在串流,则重新绑定服务并恢复界面为“运行中”状态,避免状态丢失。
This commit is contained in:
@@ -65,6 +65,10 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
screenCaptureService.setStateListener(MainActivity.this);
|
||||
setupLocalPreview();
|
||||
setupResolutionSpinner();
|
||||
// 重新绑定(如 Activity 销毁后重新打开)时,依据服务实际状态恢复界面显示为“运行中”。
|
||||
if (screenCaptureService.isStreaming()) {
|
||||
updateUI(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -121,6 +125,22 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
// MainActivity 关闭后 ScreenCaptureService 仍在后台串流。重新打开时检查服务是否正在
|
||||
// 采集/串流屏幕,若是则重新绑定并恢复界面状态,避免串流状态丢失。
|
||||
checkServiceRunningOnResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新打开 Activity 时检查后台 ScreenCaptureService 是否仍在串流/采集屏幕。
|
||||
* 若是,则重新绑定服务(不会重复创建,服务此前已作为前台服务启动),并由
|
||||
* onServiceConnected 恢复界面为“运行中”状态。
|
||||
*/
|
||||
private void checkServiceRunningOnResume() {
|
||||
ScreenCaptureService service = ScreenCaptureService.getInstance();
|
||||
if (service != null && service.isStreaming() && !isBound) {
|
||||
Intent serviceIntent = new Intent(this, ScreenCaptureService.class);
|
||||
bindService(serviceIntent, serviceConnection, BIND_AUTO_CREATE);
|
||||
}
|
||||
}
|
||||
|
||||
/** 弹窗提示开启无障碍服务;同一运行周期内仅提示一次。 */
|
||||
|
||||
@@ -317,6 +317,14 @@ public class ScreenCaptureService extends Service {
|
||||
return webRtcClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 供 MainActivity 启动/恢复时查询:服务是否正在采集并串流屏幕。
|
||||
* 用于 Activity 被销毁后重新打开时恢复界面状态,避免“服务仍在后台串流但界面显示已停止”的状态丢失问题。
|
||||
*/
|
||||
public boolean isStreaming() {
|
||||
return screenCapturer != null && !isShuttingDown;
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换屏幕采集分辨率(本地 UI 调用)。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user