1

我是 android 开发的菜鸟,我正在尝试弄清楚如何使用 Fused Location Provider。我正在使用教程,并且按照教程所示实施没有问题。但是,现在我想从意图服务中获取我的最后一个位置。问题是 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{...
4

1 回答 1

2

如何在 Intent 服务中实现 LocationClient?

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

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

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

GooglePlayServicesClient.ConnectionCallbacks 和 GooglePlayServicesClient.OnConnectionFailedListener 是用它们附带的方法实现的

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

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

于 2014-02-03T19:48:26.090 回答