如何在不锁定Android应用程序用户界面的情况下等待10秒 [英] How can I wait for 10 second without locking application UI in android

查看:341
本文介绍了如何在不锁定Android应用程序用户界面的情况下等待10秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,我想等待10秒,因为我希望我的应用程序在那10秒钟后启动下面的代码,但又不阻止该人单击应用程序中的其他任何内容(没有 >调用Thread.sleep();).

I am stuck with a problem, I want to wait 10 second because I want my application to start the code below after that 10 sec but without stopping that person from clicking anything else in the application (without calling Thread.sleep();).

try {
    Log.v("msg", "WAIT CheckFrequencyRun");
    Thread.sleep(10000); // giving time to connect to wifi
    
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   //if no network
   if(wifiManager.getConnectionInfo().getNetworkId()==-1){
    //stop wifi
    wifiManager.setWifiEnabled(false);
    Log.v("msg", "no connection");
    handler.postDelayed(this, checkInterval);
   }
   //else connection
   else{
    Log.v("msg", "connection");
    onDestroy();
   }

推荐答案

您永远不想在UI线程上调用thread.sleep(),因为听起来您已经弄清楚了.这冻结了UI,始终是一件坏事.您可以使用单独的ThreadpostDelayed

You never want to call thread.sleep() on the UI thread as it sounds like you have figured out. This freezes the UI and is always a bad thing to do. You can use a separate Thread and postDelayed

此SO答案显示了如何做到这一点以及其他几个选项

This SO answer shows how to do that as well as several other options

处理程序

TimerTask

您可以查看这些内容,看看哪种方法最适合您的特定情况

You can look at these and see which will work best for your particular situation

这篇关于如何在不锁定Android应用程序用户界面的情况下等待10秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆