2012-03-08 57 views
0

我要去找我的GPS信号,而它在寻找它,我有一个ProgressDialog,但是,它表明,当任务完成后:progressDialog并等待一个信号

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    DialogInterface.OnCancelListener dialogCancel = new DialogInterface.OnCancelListener() { 

     public void onCancel(DialogInterface dialog) { 
      Toast.makeText(getBaseContext(), 
        "no encotnrada", 
        Toast.LENGTH_LONG).show(); 
      // handler.sendEmptyMessage(0); 
     } 

    }; 

    pd = ProgressDialog.show(this, "buscando", "gps", true, true, dialogCancel); 

    while(currentLocation == null){ 

    } 

    Thread thread = new Thread(this); 
thread.start(); 
} 

中的run()我看看为我的currentLocation值。

如何等待此信号才能显示对话框?

先谢谢你

回答

1

我用GPS和GPS进程未上运行你的应用程序相同的进程中运行。 所以你的代码一定有问题,如果发现信号,GPS listener会触发一个location对象,所以你可以dismiss你的ProgressDialog

+0

你是对的!非常感谢! 它的工作! – user1256477 2012-03-09 11:17:54

+0

您必须接受答案 – 2012-03-10 23:52:01

+0

我该怎么做? 你是我想接受的人,但我不知道 – user1256477 2012-03-12 07:54:10

2

它显示在最后,因为你在主线程中循环。相反,你应该在asynctask或其他线程中等待 - 然后会显示对话框。在asynctask的onPostExecute中,您可以启动其他线程。

尝试这样:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    final WaitForIt waiter = new WaitForIt(); 

    DialogInterface.OnCancelListener dialogCancel = new DialogInterface.OnCancelListener() { 

     public void onCancel(DialogInterface dialog) { 
      Toast.makeText(getBaseContext(), 
        "no encotnrada", 
        Toast.LENGTH_LONG).show(); 
      waiter.cancelWait(); 
     } 

    }; 

    pd = ProgressDialog.show(this, "buscando", "gps", true, true, dialogCancel); 

    waiter.execute(); 
} 

private class WaitForIt extends AsyncTask<Void, Void, Void> { 
    private synchronized boolean cancelled = false; 
    public void cancelWait() { cancelled = true; } 

    protected Void doInBackground() { 
     while(!cancelled) { 
      if (gps signal available) 
       break; 
     } 
    } 

    protected void onPostExecute() { 
     pd.dismiss(); 
     Thread thread = new Thread(MyActivity.this); 
     thread.start(); 
    } 
}