android运行应用程序总是即使在后台应用程序

时间:2014-06-29 12:29:37

标签: android gps background-process

我想做以下事情:

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

2] - 我找到了一个使用LocationListener

查找GPS位置的示例

3] - 我找到了一种在设备重启时打开我的应用程序的方法

即使用户将应用程序置于后台,我也需要一些帮助才能发送此数据

这里有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

此服务应该在后台运行

LocationClient是与位置相关的API的主要入口点,例如位置和地理围栏。

使用LocationClient:

  • 连接并断开与Google位置服务的连接。
  • 请求/删除位置更新回调。
  • 请求/删除地理围栏。

要建立连接,请调用connect()并等待onConnected(android.os.Bundle)回调。

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

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

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

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() {
}

}

有用的链接:

http://developer.android.com/reference/android/app/Service.html

http://developer.android.com/guide/components/services.html

http://developer.android.com/reference/android/location/LocationListener.html

https://developer.android.com/reference/com/google/android/gms/location/LocationRequest.html

https://developer.android.com/reference/com/google/android/gms/location/LocationListener.html