java Android 服务不能作为单例工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16309494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:36:10  来源:igfitidea点击:

Android service isn't working as a singleton

javaandroidsingleton

提问by

I have a service that is being used/bound in multiple activities (I carefully wrote it so that one activity will unbind it before another binds, in onPause/onResume). However, I noticed a member in the Service won't stick....

我有一个服务正在多个活动中使用/绑定(我仔细编写了它,以便一个活动将在另一个绑定之前解除绑定,在 onPause/onResume 中)。但是,我注意到服务中的成员不会坚持......

Activity 1:

活动一:

private void bindService() {
    // Bind to QueueService
    Intent queueIntent = new Intent(this, QueueService.class);
    bindService(queueIntent, mConnection, Context.BIND_AUTO_CREATE);
}

...

bindService();

...

mService.addItems(downloads);     // the initial test adds 16 of them

Activity 2:

活动二:

bindService();                             // a different one than activity 1
int dlSize = mService.getQueue().size();   // always returns 0 (wrong)

Service's code:

服务代码:

public class QueueService extends Service {
    private ArrayList<DownloadItem> downloadItems = new ArrayList<DownloadItem();

    // omitted binders, constructor, etc

    public ArrayList<DownloadItem> addItems(ArrayList<DownloadItem> itemsToAdd) {
        downloadItems.addAll(itemsToAdd);
        return downloadItems;
    }

    public ArrayList<DownloadItem> getQueue() {
        return downloadItems;
    }
}

Upon changing one thing -- making the service's downloadItems variable into a static one -- everything works perfectly. But having to do that worries me; I've never used a singleton in this way before. Is this the correct method of using one of these?

在更改一件事后——将服务的 downloadItems 变量设为静态变量——一切正常。但不得不这样做让我很担心;我以前从未以这种方式使用过单身人士。这是使用其中之一的正确方法吗?

回答by

It turns out Nospherus was correct; all I needed to do was andd a startService()call beside my bindService()one, and all was well.

事实证明,Nospherus 是正确的;我需要做的就是startService()在我旁边打个电话bindService(),一切都很好。

Because multiple startService()calls don't call the constructor multiple times, they were exactly what I needed. (This is extremely lazy on my part, but it works for now. I am unsure of how to check for a started (and not bound) service.) My code now looks like this:

因为多次startService()调用不会多次调用构造函数,所以它们正是我所需要的。(这对我来说非常懒惰,但现在可以使用。我不确定如何检查已启动(和未绑定)的服务。)我的代码现在看起来像这样:

Intent queueIntent = new Intent(getApplicationContext(), QueueService.class);
bindService(queueIntent, mConnection, Context.BIND_AUTO_CREATE);
startService(queueIntent);

See also Bind service to activity in Android

另请参阅将服务绑定到 Android 中的活动

回答by Rohit Singh

Service is always Singleton by default

默认情况下,服务始终是单例的

Only one instance of service can ever exist at a given time. If a Service is running then there is no way you can create another instance of the Service. Period.

给定时间只能存在一个服务实例。如果服务正在运行,则您无法创建该服务的另一个实例。时期。

Binding with multiple Activity

与多个 Activity 绑定

You can bind a service to n number of Activities. Each binding works independently. When you move from one Activity to another then the changes done by Activity1will persist when you move to Activity2only if the service is alive.

您可以将服务绑定到 n 个活动。每个绑定独立工作。当您从一个 Activity 移动到另一个 ActivityActivity1时,仅当服务处于活动状态时,所做的更改才会持续Activity2存在。

enter image description here

在此处输入图片说明

Then why the changes don't persist in my case?
To understand this we should know the lifespan of a service

Lifespan of a Service

A service exist between onCreate()and onDestroy()

那么为什么这些变化在我的情况下不会持续存在?
要理解这一点,我们应该知道服务的生命周期

服务的生命周期

之间的服务存在onCreate()onDestroy()

Case 1:
enter image description hereCase 2:enter image description here

案例 1:案例 2:
在此处输入图片说明在此处输入图片说明