后台持续位置更新 [英] Continuous location updates in background

查看:223
本文介绍了后台持续位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它将不断从后台服务发送位置更新。

  public class LocationService extends Service implements LocationListener,
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient .OnConnectionFailedListener {
LocationRequest mLocationRequest;
LocationClient mLocationClient;
@Override
public void onCreate(){
//在手机中创建日志文件
appendLog(DateFormat.getDateTimeInstance().format(new Date())+:Service创建:,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();

$ b @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);


$ b @Override
public void onConnectionFailed(ConnectionResult connectionResult){
// TODO自动生成的方法存根
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自动生成的方法存根
Log.i (信息,位置客户连接);
appendLog(DateFormat.getDateTimeInstance()。format(new Date())+:Location Client Connectd:,com.example.locationservice.Constants.LOG_FILE);
//检查是否启用locaton
if(Util.isLocationEnabled(getApplicationContext())){
//检查Internet是否可用
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());


$ b @Override
public void onDisconnected(){
// TODO自动生成的方法存根
appendLog(DateFormat.getDateTimeInstance ().format(new Date())+:Location Client DisConnectd:,com.example.locationservice.Constants.LOG_FILE);
Log.i(info,Location Client Disconnected);

}

@Override
public void onLocationChanged(Location location){
// TODO自动生成的方法存根
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())){
//发送位置详细信息
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自动生成的方法存根
返回null ;

@Override
public void onDestroy(){
// TODO自动生成的方法存根
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(){
//检查Google Play服务是否可用
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(LocationService.this);
//如果Google Play服务可用
if(ConnectionResult.SUCCESS == resultCode){
return true;
} else {
return false;



code
$ b

onLocationChanged 仅在我打开内置地图应用程序时调用。另外明智的是它不更新位置细节。
我从使用警报服务的活动开始了此服务。每一分钟触发一次 alarmManager 。任何人都可以告诉我为什么onLocationChanged不会连续调用。



预先感谢。

解决方案

$ b

  @Override 
public void onCreate(){
/ /创建日志文件
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();

和:

<$ (
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5 * 1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationClient.requestLocationUpdates(mLocationRequest,this);

调用您的服务:

  startService(yourServiceIntent); 

您也可以查看我的代码这里


i am developing a application which will send location updates continuously from a background service. i tried following code.

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;
          }
         }
}

but onLocationChanged is calling only when i opend the inbuilt map application. other wise it is not updating the location details. i started this service from a activity using alarm service. alarmManager triggers for every one minute. can any one tell me why onLocationChanged is not calling continuously.

Thanks in advance.

解决方案

Try this:

@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);
 }

replace your onStart with:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mLocationClient.connect();
}

and:

@Override
public void onConnected(Bundle arg0) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(5*1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
}

Invoke your service:

startService(yourServiceIntent);

You can also check my code here

这篇关于后台持续位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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