Я использую Google Location API для моего приложения, и есть место слушатель в IT-Google Место апите местоположение слушатель
@Override
public void onLocationChanged(Location location) {
if (paused){
Runnable r = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
if (alarm){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
if(distance<rad){
alertUser();
alarm = false;
}
}
}
});
}
};
Thread bgAlarmThread = new Thread(r);
bgAlarmThread.start();
}else{
updLat = location.getLatitude();
updLng = location.getLongitude();
locll = new LatLng(updLat,updLng);
MarkerOptions locoptions = new MarkerOptions()
.title("You are here")
.position(locll)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapmarker));
if (locmarker == null){
gotoLocation(updLat, updLng, 15);
}
if(locmarker != null){
locmarker2 = locmarker;
locmarker2 = map.addMarker(locoptions);
locmarker.remove();
locmarker = null;
}
locmarker = map.addMarker(locoptions);
if(locmarker2 != null){
locmarker2.remove();
locmarker2 = null;
}
if (alarm){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
if(distance<rad){
alertUser();
Toast.makeText(this,"Destination is now within your range",Toast.LENGTH_LONG).show();
alarm = false;
}
}
if (frag2.getVisibility() == View.VISIBLE){
double distance;
Location locationA = new Location("");
locationA.setLatitude(destlat);
locationA.setLongitude(destlng);
Location locationB = new Location("");
locationB.setLatitude(updLat);
locationB.setLongitude(updLng);
distance = locationA.distanceTo(locationB);
EditText current_distance = (EditText)findViewById(R.id.currentDistance);
current_distance.setText("Current distance: " + Math.round(distance) + " m");
}
}
}
для моего приложения для работы, место слушатель должен работать, даже если приложение свернуто, но когда я нажму кнопку «домой», прослушиватель мест не будет работать. Как я могу заставить слушателя работать, когда приложение сведено к минимуму? Возможно ли запустить сам прослушиватель на альтернативном потоке? Как так?