Android show progress dialog while waiting for location

南笙酒味 提交于 2019-12-06 00:54:31

问题


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



来源:https://stackoverflow.com/questions/17671935/android-show-progress-dialog-while-waiting-for-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!