android始终运行应用程序,即使应用程序处于后台

android始终运行应用程序,即使应用程序处于后台,android,gps,background-process,Android,Gps,Background Process,我想提出以下几点: 1] -收听手机GPS位置的变化,并将其发送至服务器,以持续跟踪用户位置 2] -我找到了一个使用LocationListener查找GPS位置的示例 3] -我已找到在设备重新启动时打开应用程序的方法 我需要一些帮助,以便能够发送此数据,即使用户将应用程序放在后台 有人帮忙吗 此服务应在后台运行 是位置相关API(如位置和地理围栏)的主要入口点 使用LocationClient可以: 连接并断开与谷歌定位服务的连接 请求/删除位置更新回调 请求/移除地理围栏 为了建立连

我想提出以下几点:

1] -收听手机GPS位置的变化,并将其发送至服务器,以持续跟踪用户位置

2] -我找到了一个使用LocationListener查找GPS位置的示例

3] -我已找到在设备重新启动时打开应用程序的方法

我需要一些帮助,以便能够发送此数据,即使用户将应用程序放在后台


有人帮忙吗

此服务应在后台运行

是位置相关API(如位置和地理围栏)的主要入口点

使用LocationClient可以:

  • 连接并断开与谷歌定位服务的连接
  • 请求/删除位置更新回调
  • 请求/移除地理围栏
为了建立连接,调用connect()并等待onConnected(android.os.Bundle)回调

对象用于从LocationClient请求位置更新的服务质量

在LocationRequest中,您可以在其中设置参数,例如位置的准确性和位置更新之间的时间间隔

将根据您在LocationRequest中设置的时间间隔被调用,从那里您可以更新服务器。 该服务不在后台运行,因此您需要使用或以其他方式更新服务器,只需确保服务器更新在后台线程上完成即可

public class LocationUpdatesService extends Service implements GooglePlayServicesClient.ConnectionCallbacks,
                                                             GooglePlayServicesClient.OnConnectionFailedListener,
                                                             LocationListener {
private static int LOCATION_UPDATE_INTERVAL = 30000; // how often you will get a location update (this is in milliseconds)
private LocationClient locationClient;
private LocationRequest locationRequest;
private boolean isConnected = false;

@Override
// onCreate is called when the service gets started (from an Activity) than immediately calls onStartCommand
public void onCreate() {
    super.onCreate();

    if (servicesConnected()) {
        startLocationUpdates();
    } else {
        // isGooglePlayServicesAvailable FAILURE
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private boolean servicesConnected() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == resultCode) {
        return true;
    } else {
        return false;
    }
}

public void startLocationUpdates() {

    locationRequest = LocationRequest.create();
    locationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setFastestInterval(LOCATION_UPDATE_INTERVAL);
    locationClient = new LocationClient(this, this, this);
    locationClient.connect();
    isConnected = true;
}

@Override
public void onDestroy() {
    if (locationClient.isConnected()) {
        onDisconnectClient();
    } else {
        // locationClient is disconnected
    }
    super.onDestroy();
}

private void onDisconnectClient() {
    isConnected = false;
    locationClient.removeLocationUpdates(this);
    locationClient.disconnect();
    locationRequest = null;
    locationClient = null;
}


@Override
public void onLocationChanged(Location location) {
    // update server from here with AsyncTask (or some other way but in the background)
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnected(Bundle bundle) {
    locationClient.requestLocationUpdates(locationRequest, this);   
}

@Override
public void onDisconnected() {
}

}
有用链接:


您可以启动一项服务。但是考虑一下你的程序会对电池造成的影响。@拜占庭失败:你说的StaskBy是什么意思?你有什么例子或教程可以帮助我吗?和