停止不抛出异常Android的线程

发布于 2024-10-08 06:24:18 字数 639 浏览 0 评论 0原文

因此,我从手机中获取位置修复,如果找不到位置或花费太长时间,就会出现问题。下面是加载屏幕的示例代码

我如何设置一个计时器来在经过一定时间后停止线程(加载屏幕)?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2024-10-15 06:24:34

如果您想停止线程而不是使用普通线程执行后台作业,请使用 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 a cancel().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文