私は進行状況ダイアログには向いていません。GPSが永遠にかかると、アプリもそうなり、UXが停止するからです。ダイアログのあるアクティビティで、グローバルlocationFoundブール値、lastLongitude、およびlastLatitudeを使用してみませんか。その利点は、GPSを使用して別のアクティビティを開き、ユーザーが後方に移動した場合に、「以前の結果を表示」など、アプリの用語を実装することもできます。質問に関係のないコードを除いて、しばらく前に行った小さなアプリのアクティビティでGPSロケーション待機を実装した方法は次のとおりです。
public class MainActivity extends Activity {
TextView txt;
Button btnGPS;
Button btnSeeLast;
LocationManager locationManager;
String provider;
boolean gotLoc;
boolean hasLastLoc;
double lastLat;
double lastLon;
LocationListener myLocationListener;
@Override
public void onDestroy(){
super.onDestroy();
locationManager.removeUpdates(myLocationListener);
}
@Override
public void onCreate(Bundle savedInstanceState) {
.....
txt = (TextView)findViewById(R.id.tekst);
txt.setText("Waiting for GPS location");
btnSeeLast = (Button)findViewById(R.id.btnSeeLast);
btnSeeLast.setEnabled(false);
hasLastLoc = false;
String context = Context.LOCATION_SERVICE;
gotLoc = false;
locationManager = (LocationManager)getSystemService(context);
provider = LocationManager.GPS_PROVIDER;
myLocationListener = new UpdatesReceiver();
int t = 5000;
int distance = 5;
locationManager.requestLocationUpdates(provider, t, distance, myLocationListener);
checkGPS();
Location location = locationManager.getLastKnownLocation(provider);
if (location != null){
gotLoc = true;
updateWithNewLocation(location, true);
}
}
private void checkGPS(){
if (!locationManager.isProviderEnabled(provider)){
buildAlertMessageNoGps();
}
}
public void btnSeeLastClick(View view){
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("latitude", String.valueOf(lastLat));
intent.putExtra("longitude", String.valueOf(lastLon));
intent.putExtra("prev", false);
startActivity(intent);
}
private void updateWithNewLocation(Location location, boolean prev){
btnGPS.setEnabled(true);
String latLongString;
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
//remember
lastLat = lat;
lastLon = lng;
hasLastLoc = true;
btnSeeLast.setEnabled(true);
//end remember
latLongString = "Latitude: " + lat + "\nLongitude: " + lng;
txt.setText("");
//txt.setText(latLongString);return;
Intent intent = new Intent(this, AgentsList.class);
intent.putExtra("latitude", String.valueOf(lat));
intent.putExtra("longitude", String.valueOf(lng));
if (prev){
intent.putExtra("prev", true);
}
else{
intent.putExtra("prev", false);
}
startActivity(intent);
}
else{
latLongString = "Failed to get a response from your GPS. Try again";
txt.setText(latLongString);
}
}
private class UpdatesReceiver implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if (gotLoc == false){
gotLoc = true;
updateWithNewLocation(location, false);
}
}
@Override
public void onProviderDisabled(String provider){
// Update application if provider disabled.
}
@Override
public void onProviderEnabled(String provider){
// Update application if provider enabled.
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
// Update application if provider hardware status changed.
}
}
}