使用谷歌播放服务LocationClient在后台服务 [英] Using Google Play Services LocationClient in background service

查看:274
本文介绍了使用谷歌播放服务LocationClient在后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序被设计为定期跟踪用户的位置,并将其发送到服务器,最近,我改变了我的code与谷歌播放服务位置API。

我创建的locationclient和被连接到业务中onStartCommand

 公众诠释onStartCommand(意向意图,诠释标志,INT startId){
    setUpLocationClientIfNeeded();
    如果(!mLocationClient.isConnected()||!mLocationClient.isConnecting())
    mLocationClient.connect();
    返回START_STICKY;

}
 

和在onConnected方法,我发送定位请求,

  @覆盖
公共无效onConnected(捆绑为arg0){
    的System.out.println(已连接......);
    mLocationClient.requestLocationUpdates(要求,这一点);

}
 

Request对象,

 私有静态最后LocationRequest REQUEST = LocationRequest.create()
      .setInterval(5 * 60 * 1000)//5分钟
      .setFastestInterval(3 * 60 * 1000)// 3分钟
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
 

现在的问题是,

  1. 的onLocationChanged方法是没有得到所谓的在给定的时间间隔,即5分钟或最快的时间间隔3分钟。从日志中我可以看到,它的获取调用只有两次或三次之后,它没有得到所谓的在所有(我1小时后检查)。

什么是问题,我上面的code? (我不能看到任何日志断开连接也可以)

  1. 要解决这个问题,我试图用alarmmanager定期调用任务。现在,如何通过Locationclient从一个BroadcastReceiver得到一个位置更新。 (locationclient.getLastlocation()只返回最后存储的位置,但它不请求新位置)
解决方案

完整的源$ C ​​$ C的后台服务可以在这里找到:

<一个href="https://gist.github.com/blackcj/20efe2ac885c7297a676">https://gist.github.com/blackcj/20efe2ac885c7297a676

尝试添加超级调用你的onStartCommand。

  / **
 *保持即使在应用程序被关闭运行的服务。
 *
 * /
公众诠释onStartCommand(意向意图,诠释标志,INT startId)
{
    super.onStartCommand(意向,标志,startId);

    setUpLocationClientIfNeeded();
    如果(!mLocationClient.isConnected()||!mLocationClient.isConnecting())
    {
        mLocationClient.connect();
    }

    返回START_STICKY;
}
 

My app is designed to track user's location periodically and send it to server, Recently I changed my code with Google play services Location API.

I created the locationclient and connected to the service in onStartCommand

public int onStartCommand(Intent intent, int flags, int startId) {
    setUpLocationClientIfNeeded();
    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting())
    mLocationClient.connect();
    return START_STICKY;

}

and in onConnected method, I send a location request,

@Override
public void onConnected(Bundle arg0) {
    System.out.println("Connected ...");
    mLocationClient.requestLocationUpdates(REQUEST, this);

}

The REQUEST object is,

 private static final LocationRequest REQUEST = LocationRequest.create()
      .setInterval(5*60*1000)      // 5 minutes
      .setFastestInterval(3*60*1000) // 3 minutes
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

Now the issue is,

  1. the onLocationChanged method is not getting called at the given interval i.e 5 minutes or the fastest interval 3 minutes. From the log I could see, its getting called only twice or thrice after that its not getting called at all ( I checked after 1 hour).

What is the issue with my above code?. ( I couldnt see any log for 'disconnected' also)

  1. To solve this, I tried to use alarmmanager to call the task periodically. Now how to get a single location update through Locationclient from a broadcastreceiver. (locationclient.getLastlocation() only return last stored location but it is not requesting a new location)

解决方案

Full source code for a background service available here:

https://gist.github.com/blackcj/20efe2ac885c7297a676

Try adding the super call to your onStartCommand.

/**
 * Keeps the service running even after the app is closed.
 * 
 */
public int onStartCommand (Intent intent, int flags, int startId)
{
    super.onStartCommand(intent, flags, startId);

    setUpLocationClientIfNeeded();
    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting())
    {
        mLocationClient.connect();
    }

    return START_STICKY;
}

这篇关于使用谷歌播放服务LocationClient在后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆