Android show progress dialog while waiting for location

China☆狼群 提交于 2019-12-04 07:29:27

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

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

}
Shankari vatsalkumar

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();
}
canova

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

Displaying a ProgressDialog while waiting for a joined Thread

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