我正在尝试编写一个使用RTSP播放流式音频的简单媒体播放器.我有一个GUI活动和一个执行播放的服务.我的问题是如何在活动和服务之间进行最佳沟通(例如,根据玩家状态更新gui).
我知道我可以使用onBind()将服务绑定到活动,但如果我理解正确,如果活动被终止,这将停止服务.即使用户退出活动,我也想继续播放.有没有任何标准或首选的方法来处理这个问题?
从 stackoverflow 和许多博客中,我确信前台服务在 API>25 中永远不会在没有通知的情况下运行。但我仍然感到困惑,当应用程序在屏幕上运行或可见时,通知是否是强制的。例如。当用户站在应用程序内时无需通知。那么这可以在应用程序运行时删除通知吗? 在服务舱
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
......
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
}
return START_NOT_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
活动中
Intent myService = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(myService);
} else {
startService(myService);
}
Run Code Online (Sandbox Code Playgroud)