【问题标题】:Fused location provider singleton example?融合位置提供者单例示例?
【发布时间】:2015-05-20 16:54:57
【问题描述】:

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

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

【问题讨论】:

    标签: android fusedlocationproviderapi


    【解决方案1】:

    这是可能的。您将需要实现私有构造函数/ 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 约定,但是像这样构建代码应该可以工作。

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 1970-01-01
      • 2013-02-06
      • 2013-08-02
      • 1970-01-01
      • 2017-04-02
      • 2013-09-01
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多