停止不抛出异常Android的线程
因此,我从手机中获取位置修复,如果找不到位置或花费太长时间,就会出现问题。下面是加载屏幕的示例代码
我如何设置一个计时器来在经过一定时间后停止线程(加载屏幕)?
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 10f, 这个);
处理程序处理程序 = new Handler(); Runnable showWaitDialog = new Runnable() { int 等待 = 0; @覆盖 公共无效运行(){ while (loc == null) {
}
// After receiving first GPS Fix dismiss the Progress Dialog
dialog.dismiss();
} };
// 创建一个对话框让用户知道我们正在等待 GPS 修复 dialog = ProgressDialog.show(Weather.this, "请稍等...", “正在检索 GPS 数据...”,true);
线程 t = 新线程(showWaitDialog); t.start();
So Im getting the location fix from the phone and the problem comes up if it never finds the location or takes wayy too long. below is the sample code from the loading screen
How could i set up a timer to stop the thread(load screen) once a certain amount of time has passed?
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 10f, this);
Handler handler = new Handler();
Runnable showWaitDialog = new Runnable() {
int wait = 0;
@Override
public void run() {
while (loc == null) {
}
// After receiving first GPS Fix dismiss the Progress Dialog
dialog.dismiss();
}
};
// Create a Dialog to let the User know that we're waiting for a GPS Fix
dialog = ProgressDialog.show(Weather.this, "Please wait...",
"Retrieving GPS data ...", true);
Thread t = new Thread(showWaitDialog);
t.start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想停止线程而不是使用普通线程执行后台作业,请使用 Android sdk 的
AsyncTask
,在那里你可以找到一个cancel()
。If you want to stop a thread instead of using normal thread to do background jobs use Android sdk's
AsyncTask
, there you can find acancel()
.