Android show progress dialog while waiting for loc

2019-04-08 23:18发布

问题:

I'm developing location based app using this example: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

But when I turn on phone, the location isn't available right on that moment. So I would like to show progress dialog while waiting for the location. Wanna do this in the background using AsyncTask.

Can you give any ideas how and where to do that?

回答1:

There is no need of AsyncTask because Location service already running in different process, Just implement the LocationListener and register it on resume method, and in onCreateActivity check if location is null, the show the ProgressDialog, and in onLocationChanged() set the location and close the ProgressDialog



回答2:

Implement locationListner interface and start your wait dialog override onlocation change method and there just cancel the dialog, all the best.

public class MainActivity extends Activity implements LocationListener{

ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //-------------------- Start your GPS Reading ------------------ //
    dialog = new ProgressDialog(this);
    dialog.setMessage("Please wait!");
    dialog.show();

}
@Override
public void onLocationChanged(Location arg0) {
    dialog.dismiss();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}


回答3:

Place your ProgressDialog in onPreExecute, sample code below:

private ProgressDialog progressdialog;

@Override
protected void onPreExecute(){ 
   super.onPreExecute();
        progressdialog = new ProgressDialog(yourContext);
        progressdialog.setMessage("Loading...");
        progressdialog.show();    
}

@Override
protected void onPostExecute(){
   super.onPostExecute();
        progressdialog.dismiss();
}


回答4:

I wrote a solution for using ProgressDialog in AsyncTask on the following topic:

Displaying a ProgressDialog while waiting for a joined Thread