为应用程序开发一些签入功能,并尝试找出处理 lastKnowLocation 为空的最干净的方法。目前,我附加了一个位置监听器,然后启动了一个异步任务,该任务发布了一个不确定的进度对话框,提醒用户我正在搜索 gps 以收集他们的位置;doInBackground 方法本质上只是一个繁忙的等待循环,等待位置不为空。作为免责声明,我觉得从代码的角度来看,当前的实现有点“笨拙”,但提供了预期的用户体验。但是,我想知道是否有人可以提供有关处理这种相当常见情况的更好方法的见解。
谢谢
为应用程序开发一些签入功能,并尝试找出处理 lastKnowLocation 为空的最干净的方法。目前,我附加了一个位置监听器,然后启动了一个异步任务,该任务发布了一个不确定的进度对话框,提醒用户我正在搜索 gps 以收集他们的位置;doInBackground 方法本质上只是一个繁忙的等待循环,等待位置不为空。作为免责声明,我觉得从代码的角度来看,当前的实现有点“笨拙”,但提供了预期的用户体验。但是,我想知道是否有人可以提供有关处理这种相当常见情况的更好方法的见解。
谢谢
I'm not for the progress dialog because if the GPS takes forever then so will your app, killing the UX. In the activity with the dialog why not have a global locationFound boolean, a lastLongitude and lastLatitude. The advantage of that being if the you use the GPS returned to open another activity and the user navigates backwards you can even implement a "See previous results" or whatever the terminology is for your app. Here's how i implemented a GPS location wait in an activity for a small app i did a while ago, excluding the code that doesn't matter for your question
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.
}
}
}
如果您现在想要该位置,请使用 getLastKnownLocation。如果您正在等待它为非 null,则应改为 requestLocationUpdates。如果因为您不想要超过 1 次更新而过大,请使用 requestSingleUpdate。