gpt4 book ai didi

java - 我应该绑定(bind)我的服务还是多次调用 startService ?

转载 作者:行者123 更新时间:2023-12-02 03:42:14 26 4
gpt4 key购买 nike

我通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com