在后台持续更新位置
Posted
技术标签:
【中文标题】在后台持续更新位置【英文标题】:Continuous location updates in background 【发布时间】:2014-03-10 16:53:45 【问题描述】:我正在开发一个应用程序,它将从后台服务连续发送位置更新。我尝试了以下代码。
public class LocationService extends Service implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
LocationRequest mLocationRequest;
LocationClient mLocationClient;
@Override
public void onCreate()
//creating log file in mobile
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service created:", com.example.locationservice.Constants.LOG_FILE);
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5*1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// mLocationRequest.setFastestInterval(5*1000);
mLocationClient = new LocationClient(getApplicationContext(), this,this);
mLocationClient.connect();
@Override
public void onStart(Intent intent, int startId)
int start = Service.START_STICKY;
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service Started:", com.example.locationservice.Constants.LOG_FILE);
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
// TODO Auto-generated method stub
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Connection to client failed", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
@Override
public void onConnected(Bundle arg0)
// TODO Auto-generated method stub
Log.i("info", "Location Client is Connected");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Client Connectd:", com.example.locationservice.Constants.LOG_FILE);
//checking for locaton enabled or not
if(Util.isLocationEnabled(getApplicationContext()))
//checking for internet available or not
if(Util.isInternetOn(getApplicationContext()))
mLocationClient.requestLocationUpdates(mLocationRequest, this);
else
Log.i("info", "Internet not available");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Internet not available", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
else
Log.i("info", "Location Acess disabled");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Acess disabled", com.example.locationservice.Constants.LOG_FILE);
this.stopSelf();
Log.i("info", "Service Connect status :: " + isServicesConnected());
@Override
public void onDisconnected()
// TODO Auto-generated method stub
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Client DisConnectd:", com.example.locationservice.Constants.LOG_FILE);
Log.i("info", "Location Client is DisConnected");
@Override
public void onLocationChanged(Location location)
// TODO Auto-generated method stub
double latitude = location.getLatitude();
double longitude = location.getLongitude();
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Location Changed:", com.example.locationservice.Constants.LOG_FILE);
Log.i("info", "Latitude :: " + latitude);
Log.i("info", "Longitude :: " + longitude);
if(Util.isInternetOn(getApplicationContext()))
//sending location details
sendLocation(location);
else
this.stopSelf();
Log.i("info", "Internet not available");
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Internet not available", com.example.locationservice.Constants.LOG_FILE);
@Override
public IBinder onBind(Intent intent)
// TODO Auto-generated method stub
return null;
@Override
public void onDestroy()
// TODO Auto-generated method stub
Log.i("info", "Service is destroyed");
mLocationClient.removeLocationUpdates(this);
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service Destroyed:", com.example.locationservice.Constants.LOG_FILE);
super.onDestroy();
private boolean isServicesConnected()
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationService.this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode)
return true;
else
return false;
但onLocationChanged
仅在我打开内置地图应用程序时调用。否则,它不会更新位置详细信息。
我从使用警报服务的活动中启动了此服务。 alarmManager
每一分钟触发一次。谁能告诉我为什么 onLocationChanged 没有连续调用。
提前致谢。
【问题讨论】:
您确定设备有时间获得 GPS 首次定位(如您所期望的高精度)。如果它一直困扰着您,您可以从这个答案***.com/questions/19365035/… 中尝试我的代码。已经在多个场合成功使用过。 【参考方案1】:试试这个:
@Override
public void onCreate()
//creating log file in mobile
appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service created:", com.example.locationservice.Constants.LOG_FILE);
mLocationClient = new LocationClient(getApplicationContext(), this,this);
将您的 onStart 替换为:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
mLocationClient.connect();
和:
@Override
public void onConnected(Bundle arg0)
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5*1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
调用你的服务:
startService(yourServiceIntent);
你也可以查看我的代码here
【讨论】:
后台运行正常吗?在后台运行时服务在 45 分钟后不会停止?【参考方案2】:它在后台工作正常吗?在后台运行时服务在 45 分钟后没有停止? – 诺曼 2015 年 11 月 3 日 6:18
这是一个很好的评论。在我的项目中,我对位置更新有一些问题。 45 分钟后它真的停止了。
我的服务是前台。我用
StartForeground(ServicesId.Push, 通知);
而且我每 45 分钟醒来一次位置更新
_timer = new Timer(); _timer.Schedule(new TimerTaskTrek(this), 45 * 60 * 1000 + 10 * 1000, 45 * 60 * 1000 + 10 * 1000);
在定时器任务中
试试
Looper.Prepare();
捕获(异常 e)
_client = new >GoogleApiClient.Builder(this).AddApi(LocationServices.API) .AddConnectionCallbacks(这个) .AddOnConnectionFailedListener(这个) .Build();
_client.Connect();
这很简单,而且效果很好。 享受
【讨论】:
以上是关于在后台持续更新位置的主要内容,如果未能解决你的问题,请参考以下文章