【问题标题】:How Do I Implement LocationClient within Intent Service?如何在 Intent 服务中实现 LocationClient?
【发布时间】:2014-02-03 19:40:07
【问题描述】:

我是 android 开发的菜鸟,我正在尝试弄清楚如何使用 Fused Location Provider。我正在使用this 教程,并且按照教程所示实现没有问题。但是,现在我想从意图服务中获取我的最后一个位置。问题是 Location Client 的新实例不接受 context 或 context.getApplicationContext() 作为 ConnectionCallbacks 和 ConnectionFailedListner 的参数。 GooglePlayServicesClient.ConnectionCallbacks 和 GooglePlayServicesClient.OnConnectionFailedListener 是通过它们附带的方法实现的。非常感谢任何帮助。

我的代码

public class GCMIntentService extends GCMBaseIntentService implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener,LocationListener{...

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    //Checking for message type
    //Received geofence coordinates
    if (message.contains("Your parent is acquiring your current location")){

        c = context;
    //Force get location                
    locationclient = new LocationClient(c,c,c);//<--Not working 
    locationclient = new LocationClient(c,c.getApplicationContext(),c.getApplicationContext());//<--Not working 
    locationrequest = LocationRequest.create();   
    locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);                locationclient.connect();


    }else{...

【问题讨论】:

    标签: android location google-maps-android-api-2 google-play-services location-provider


    【解决方案1】:

    如何在 Intent 服务中实现 LocationClient?

    你不会的。 LocationClient 的 API 是异步的,不能很好地与 IntentService 配合使用。使用普通的Service,根据需要管理自己的后台线程,不再需要服务时调用stopSelf()

    问题是 Location Client 的新实例不接受 context 或 context.getApplicationContext() 作为 ConnectionCallbacks 和 ConnectionFailedListner 的参数。

    您必须传入这些侦听器接口的实现。

    GooglePlayServicesClient.ConnectionCallbacks 和 GooglePlayServicesClient.OnConnectionFailedListener 是通过其随附的方法实现的

    不在ContextApplication 上。这些类不是你写的——谷歌写的。因此,您无法在这些类上实现这些接口。

    也许,您在某个地方确实实现了这些接口,在这种情况下,您需要将该对象(或多个对象,复数)的实例传递给适当的方法。

    【讨论】:

    • 您给出的前两个答案对我来说似乎很清楚。但是当您说“...您需要将该对象(或多个对象,复数)的实例传递给适当的方法”时,我不明白。我认为这种方法在 Android 中是一个非常基本的概念——但就像我说的,我不能将它应用到我的程序中。你对我有什么好的建议吗?如果您有任何好的网站可以让我了解更多相关信息,请告诉我。
    • @MartinPfeffer:“你对我有什么好的建议吗?” -- ummmmm...对象、方法、类和接口是Java 的核心概念。 具体你不明白什么?
    • 好吧,实际上我已经设置了我的应用程序以接收位置更新(在我的后台服务中),这工作正常,但是当我禁用手机的位置设置时 onDisconnected 不会被调用。所以我认为我必须在其他地方实现这个监听器——而不是在服务中。这是正确的吗?
    • 好吧...阅读文档让我有了一个新的“想法”,我的错误可能是什么。 onDisconneced -> "当客户端断开连接时调用。如果远程服务出现问题(例如崩溃或资源问题导致它被系统杀死),可能会发生这种情况。调用时,所有请求都已取消并且没有出色的侦听器将被执行。” ...但是我该如何模拟这种情况?
    • @MartinPfeffer:“onDisconnected 没有被调用”——也许你没有被断开。我不会假设在您描述的情况下会调用onDisconnected()。打个比方:你正在和某人通电话。那人放下电话,走开了。您的电话没有断开,因为您和对方都没有挂断电话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2019-02-15
    • 2015-02-06
    相关资源
    最近更新 更多