0
我使用谷歌API的位置我的應用程序並且在它 -谷歌的位置API位置監聽器
@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");
}
}
}
位置監聽器對於我的應用程序工作,位置監聽器應該工作,即使應用程序最小化,但是當我點擊主頁按鈕時,位置偵聽器將無法工作。如何在應用程序最小化時讓偵聽器工作?是否有可能在另一個線程上運行偵聽器本身?怎麼會這樣?