2012-03-08 92 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(); 
    } 
}