- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过 Intent 在我的 Activity 和我的服务之间发送数据(我多次调用 startForegroundService 因为它再次调用 onStartCommand 并使用 getIntent 我获取我的数据)。
示例:
Intent intent = new Intent(getActivity(), MediaPlayerService.class);
intent.putParcelableArrayListExtra("songList", Main.musicList);
intent.putExtra("songIndex", position);
intent.setAction(Constants.ACTIONS.ACTION_PLAY);
ContextCompat.startForegroundService(getActivity(), intent);
我在某处读到,最好通过绑定(bind)与我的服务进行通信。
但是我用这种方法实现了同样的效果,那么哪一个更可取?
最佳答案
来自文档:
Started
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
Bound
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
您可以阅读更多相关内容@:Android Services , Bound Services
关于java - 我应该绑定(bind)我的服务还是多次调用 startService ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829191/
Context.startService(intent) 和 startService(intent) 有什么区别,使用哪个有关系吗? 最佳答案 只有一个startService() 方法。 star
这只是一个知识问题,我知道这样做不是一个好的做法。所以这里是: 如果我调用 startService() 3 次而它已经在运行,我是否必须调用 stopService() 3 次才能让它停止? (它有
我有一个依赖 X 的 Windows 服务 S1。该服务是自动的,它可以毫无问题地启动。 此服务启动另一个具有相同依赖项 X 的服务 S2。问题是,当我调用 StartService() 启动 S2
我尝试从服务类启动 android 服务。这样做的原因是为了实现一些平台独立性。 这样做我在 android.content.ContextWrapper.startService(ContextWr
我有这个函数作为静态方法: public static void engine(SQLiteDatabase mydb){ Intent intent = new Intent
我在文档中的任何地方都找不到这个: 对 context.startService() 的调用是同步的还是异步的? 最佳答案 异步。它甚至不会开始做任何工作,直到您从您所在的任何回调(例如,onCrea
startService() 是创建一个新的服务实例还是使用现有的?例如,在下面的代码中,它是创建两个 UpdateService 实例还是只创建一个 UpdateService 实例?谢谢。
我正在尝试安装 Neo4j(1.8),但在启动服务时遇到问题。 解压zip文件后,我直接进入bin文件夹并执行批处理文件: Neo4J.bat install [SC] 创建服务成功 [SC] 启动
我正在 Delphi 10.2 pro 中编写一些服务应用程序,我想添加一个启动时间可控参数来强制服务应用程序进入启动等待循环足够长的时间,以便我可以单击进入“运行\附加到进程”窗口(在应用程序开始初
我有一个小脚本,用户可以在其中输入脚本名称(描述名称),并可以选择修改属性。这是通过GUI界面完成的:一个文本框和一些下拉列表。 我尝试了几种方法来实现它,但是没有一个起作用。 这是我如何检索服务信息
这件事让我非常头疼。 我正在尝试使用 CreateService 和 SERVICE_ALL_ACCESS 将驱动程序加载到进程中,在我实际尝试使用 StartService 启动服务之前,一切似乎都
这就是我启动位置服务的方式。 private void initLocationService() { locationManager = (LocationManager) getSyste
我已经创建了一个服务,我正在使用 bindService 函数通过我的 Activity 访问它,但我想在我的 Activity 退出后继续运行我的服务。 为此,我必须使用 startService,
我正在尝试在Windows 10(10.0.18362)上安装自签名内核驱动程序,这里是我遵循的步骤以及出现的错误。如何解决这个问题?谢谢 c:\>makecert -r -pe -ss Privat
我想从我的 Activity 类(class)中启动一项服务。问题是,onStartCommand 永远不会被调用。我在那里放置了断点,但它从未被击中。我的例子非常简单,我不明白为什么它不起作用。 M
我通过 Intent 在我的 Activity 和我的服务之间发送数据(我多次调用 startForegroundService 因为它再次调用 onStartCommand 并使用 getInten
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我正在尝试使用 IntentSe
在 while(true) 语句中,我正在创建一个将在 onHandleIntent 中运行案例的 Intent 。但是,如果不将 Activity 类扩展到我的 LocationTracker 类,
我想用 C++ 程序启动卷影复制服务。这涉及获取服务控制管理器 (OpenSCManager) 的句柄,使用它来获取服务句柄 (OpenService),然后启动服务 (StartService)。
我继承了一个项目,但我不明白如何通过以下代码获得响应或设置回调: startService(new Intent(this, VenueUpdateService.class)); 目前,即使 Ven
我是一名优秀的程序员,十分优秀!