简体   繁体   English

Android在等待位置时显示进度对话框

[英]Android show progress dialog while waiting for location

I'm developing location based app using this example: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ 我正在使用此示例开发基于位置的应用程序: 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. 想在后台使用AsyncTask执行此操作。

Can you give any ideas how and where to do that? 您能否提出任何想法以及如何做到这一点?

不需要AsyncTask,因为Location服务已经在不同的进程中运行,只需实现LocationListener并在resume方法上注册它,并在onCreateActivity中检查location是否为null,显示ProgressDialog ,并在onLocationChanged()设置位置并关闭ProgressDialog

Place your ProgressDialog in onPreExecute, sample code below: 将ProgressDialog放在onPreExecute中,示例代码如下:

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

Implement locationListner interface and start your wait dialog override onlocation change method and there just cancel the dialog, all the best. 实现locationListner接口并启动等待对话框覆盖onlocation更改方法,只需取消对话框,一切顺利。

public class MainActivity extends Activity implements LocationListener{ 公共类MainActivity扩展Activity实现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

}

I wrote a solution for using ProgressDialog in AsyncTask on the following topic: 我在以下主题上编写了一个在AsyncTask中使用ProgressDialog的解决方案:

Displaying a ProgressDialog while waiting for a joined Thread 在等待连接的线程时显示ProgressDialog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM