我想寻找我的GPS信号,而它正在寻找它我有一个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值。
我如何等待此信号才能显示对话框?
提前谢谢
答案 0 :(得分: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();
}
}
答案 1 :(得分:1)
我已经使用了GPS,并且GPS进程没有运行与运行应用程序相同的进程。
所以代码一定有问题,如果找到信号,GPS listener
会触发location
个对象,因此您可dismiss
ProgressDialog
。