Thi*_*gaB 38 android location android-service google-play-services
我的应用程序旨在定期跟踪用户的位置并将其发送到服务器,最近我使用Google Play服务位置API更改了我的代码.
我创建了locationclient并连接到onStartCommand中的服务
public int onStartCommand(Intent intent, int flags, int startId) {
setUpLocationClientIfNeeded();
if(!mLocationClient.isConnected() || !mLocationClient.isConnecting())
mLocationClient.connect();
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
在onConnected方法中,我发送一个位置请求,
@Override
public void onConnected(Bundle arg0) {
System.out.println("Connected ...");
mLocationClient.requestLocationUpdates(REQUEST, this);
}
Run Code Online (Sandbox Code Playgroud)
REQUEST对象是,
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5*60*1000) // 5 minutes
.setFastestInterval(3*60*1000) // 3 minutes
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Run Code Online (Sandbox Code Playgroud)
现在的问题是,
我上面的代码有什么问题?(我也看不到'断开'的任何日志)
bla*_*kcj 35
此处提供后台服务的完整源代码:
https://gist.github.com/blackcj/20efe2ac885c7297a676
尝试将超级调用添加到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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18022 次 |
| 最近记录: |