Android 安卓定位服务没有';我不在后台工作
我正在开发一个定位应用程序,当用户按下Android 安卓定位服务没有';我不在后台工作,android,service,background,location,Android,Service,Background,Location,我正在开发一个定位应用程序,当用户按下按钮时,它应该开始跟踪一些基于经度和纬度的统计数据。所有这些东西都很好用,但是当需要锁定屏幕或将应用程序放在后台时,服务就不再起作用了 我读过很多关于后台服务和广播接收器的书,但我不知道如何在服务中实现Google API位置侦听器,也不知道在MainActivity中在哪里实现这个类。有人能用一个简短的代码示例告诉我如何实现这样的服务或链接吗 下面是一个应用程序,它可以实现您想要的功能: 主要活动从背景录像机服务开始 Intent intent = ne
按钮时,它应该开始跟踪一些基于经度和纬度的统计数据。所有这些东西都很好用,但是当需要锁定屏幕或将应用程序放在后台时,服务就不再起作用了
我读过很多关于后台服务和广播接收器的书,但我不知道如何在服务中实现Google API位置侦听器,也不知道在MainActivity
中在哪里实现这个类。有人能用一个简短的代码示例告诉我如何实现这样的服务或链接吗 下面是一个应用程序,它可以实现您想要的功能:
主要活动从背景录像机服务开始
Intent intent = new Intent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
看看mConnection on服务已连接,on服务已断开
以下是录像机课程的背景:
它实现LocationListener。因此,OnLocation已更改、onProviderDisabled、onProviderEnabled、OnStatus已更改。使用fuse定位服务,并每次保存更新的位置
public class LocationNotifyService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
public static Location mCurrentLocation;
.............................
@Override
public void onCreate() {
//show error dialog if GoolglePlayServices not available
if (isGooglePlayServicesAvailable()) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//mLocationRequest.setSmallestDisplacement(10.0f); /* min dist for location change, here it is 10 meter */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
//Check Google play is available or not
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
return ConnectionResult.SUCCESS == status;
}
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
protected void startLocationUpdates() {
try {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
} catch (IllegalStateException e) {}
}
@Override
public void onLocationChanged(Location location) {
//Save your location
}
............................
}
公共类LocationNotifyService扩展了服务实现
LocationListener,
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener{
位置请求mLocationRequest;
GoogleapClient MGoogleapClient;
公共静态位置mCurrentLocation;
.............................
@凌驾
public void onCreate(){
//如果GoolglePlayServices不可用,则显示错误对话框
如果(isGooglePlayServicesAvailable()){
mlLocationRequest=新位置请求();
mlLocationRequest.setInterval(后台间隔);
mlLocationRequest.SetFastTestInterval(背景时间间隔);
mLocationRequest.setPriority(位置请求.优先级高精度);
//M位置请求。设置最小位移(10.0f);/*位置更改的最小距离,此处为10米*/
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
mGoogleApiClient.connect();
}
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
返回super.onStartCommand(intent、flags、startId);
}
//检查Google play是否可用
私有布尔值isGooglePlayServicesAvailable(){
int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
返回ConnectionResult.SUCCESS==状态;
}
@凌驾
未连接的公共空间(捆绑包){
startLocationUpdates();
}
受保护的void startLocationUpdates(){
试一试{
Pendingreult Pendingreult=LocationServices.FusedLocationApi.RequestLocationUpdate(
mgoogleapclient,mLocationRequest,this);
}捕获(非法状态){}
}
@凌驾
已更改位置上的公共无效(位置){
//保存您的位置
}
............................
}
您可以获取当前位置onLocationChanged()
有关更多详细信息,请查看或遵循官方指南谢谢,这正是我想要的!如何在MainActivity中调用此服务以显示数据?如何在MainActivity中调用接收器以及如何处理该服务在MainActivity中提供的数据?将位置lat long保存在SharedReference中并继续编辑它onLocationChanged()方法。只需调用startService(newintent(YourActivity.this,LocationNotifyService.class)),就可以从活动启动服务代码>我应该在MainActivity中的哪个函数中调用包含最新位置的SharedReferences?是否有一个函数可以在服务(位置)发生变化时触发?@madrid11如何在特定时间触发此函数