我正在尝试构建一个Android应用,在设备启动的整个过程中在后台获取位置。 它用于商业环境并且永久供电,因此电池消耗无关紧要。
在设备启动时,我启用了PARTIAL_WAKE_LOCK,并启动了一个开始请求位置更新的位置服务。
Intent intentLocation = new Intent(context, LocationService.class);
context.startService(intentLocation);
在onLocationChanged方法中,我正在处理数据。
但是经过一段随机的时间,可能是半天,或者几天,该应用程序停止获取位置更新。
当应用停止获取位置更新时,我需要重新启动设备或停止应用并重新启动它。
即使我已经指定了START_STICKY,我还是假设Android正在终止进程并且没有正确重启?
我是否需要在另一个线程中获取位置数据,或者我还能做些什么来确保位置更新继续通过?
这是我的位置课程。
public class LocationService extends Service implements LocationListener {
public Context context;
private static final String TAG = "LocationService";
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location; // location
// Declaring a Location Manager
protected LocationManager locationManager;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 10000;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
/*
* Callback that fires when the location changes.
*/
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged called");
locationBroadcast(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
@SuppressWarnings("MissingPermission")
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
Toast.makeText(getApplicationContext(), "Turn on location services", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d(TAG, "Network GPS provider being used");
}
// if GPS Enabled get location using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d(TAG, "GPS provider being used");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "Received start id " + startId + ": " + intent);
return START_STICKY;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
context = this;
getLocation();
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
locationManager.removeUpdates(this);
}
private void locationBroadcast(Location currentLocation) {
Intent intent = new Intent("gpsdata");
sendLocationBroadcast(intent, currentLocation);
}
private void sendLocationBroadcast(Intent intent, Location currentLocation){
JSONObject locationData = new JSONObject();
try {
locationData.put("latitude", currentLocation.getLatitude());
locationData.put("longitude", currentLocation.getLongitude());
locationData.put("altitude", Math.round(currentLocation.getAltitude()));
locationData.put("accuracy", Math.round(currentLocation.getAccuracy()));
locationData.put("speed", Math.round(currentLocation.getSpeed()));
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
intent.putExtra("locationobject", locationData.toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}