0

嗨,是否可以创建一个融合的位置提供程序单例,可用于从许多活动中获取最后一个位置?

此外,在连接和断开连接方面,我将如何在这些活动中维护一个 Google Api 客户端实例。

4

1 回答 1

0

有可能的。您将需要实现私有构造函数/ getInstance() 组合——

/**
 * Private constructor.
 */   
private GPSPlotter(Context theContext) {
    initializeInstance();
    initializeFields(theContext);
    buildApiClient();
    connectClient();
}

/**
 * Returns an instance of the GPS Plotter.
 */
public static GPSPlotter getInstance(Context theContext) {

    if (gpsPlotterInstance == null)
        return new GPSPlotter(theContext);
    else
        return gpsPlotterInstance;

}

我相当肯定将 Context 作为参数传递给模型对象违反了 Android 约定,但是像这样构建代码应该可以工作。

于 2015-06-18T17:48:41.870 回答