2

我知道 Singleton,但我不能在 Android 项目中使用它。我是Android的初学者。请告诉我如何以及在哪里可以在 Android 大数据项目中使用 Singleton。我已经将它用于简单的值。

4

2 回答 2

41

Android 中的 Singleton 与 Java 中的 Singleton 相同:

一个基本的 Singleton 类示例:

public class AppManager
{
    private static AppManager   _instance;

    private AppManager()
    {

    }

    public synchronized static AppManager getInstance()
    {
        if (_instance == null)
        {
            _instance = new AppManager();
        }
        return _instance;
    }
}
于 2012-09-25T16:23:45.700 回答
4
  1. 对于“大数据”,请使用数据库。Android 为您提供 SQlite。
  2. 当然你可以在 Android 中使用 Singletons。是什么让你认为你不能?

有关单例模式的更多信息,请阅读http://en.wikipedia.org/wiki/Singleton_pattern

有关 android 中 SQLite 的更多信息,请阅读:http: //developer.android.com/guide/topics/data/data-storage.html#db

于 2012-09-25T15:03:58.483 回答