如何在Intent服务中实现LocationClient?

时间:2014-02-03 19:40:07

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

我是Android开发的菜鸟,我正在试图弄清楚如何使用Fused Location Provider。我正在使用this教程,并且没有像教程所示那样实现问题。但是,现在我想从意图服务中获取我的最后一个位置。问题是,Location Client的新实例不接受上下文或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{...

1 个答案:

答案 0 :(得分:2)

  

如何在Intent服务中实现LocationClient?

你不会。 LocationClient的API是异步的,但与IntentService不兼容。使用常规Service,根据需要管理自己的后台主题,并在不再需要该服务时致电stopSelf()

  

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

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

  使用随附的方法实现了GooglePlayServicesClient.ConnectionCallbacks和GooglePlayServicesClient.OnConnectionFailedListener

不在Context而不在Application。你没有写这些类 - 谷歌做了。因此,您无法在这些类上实现这些接口。

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

相关问题