【问题标题】:How to delay some action in Android so that a task completes after a set Delay in Android如何延迟 Android 中的某些操作,以便在 Android 中设置延迟后完成任务
【发布时间】:2021-07-12 09:36:04
【问题描述】:

我正在尝试在 Android 中开发一个应用程序,它有一些需要在一段时间后执行的操作。

我能做些什么来实现这一点?

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您使用的是 Java,则使用 Android Handler 来延迟您的任务

    在您的活动的 onCreate 方法中执行以下操作:

     int any_delay_in_ms = 1000; //1Second interval
        new Handler().postDelayed(() -> {
            //TODO Perform your action here
        }, any_delay_in_ms);
    

    【讨论】:

    • 好的,我试试这个
    【解决方案2】:
        int delay = 1000;//1 sec, change it as per need
        //if you are not executing anything which need main thread 
        new Handler().postDelayed(() -> {
    
        }, delay);
        
        //if you are  executing anything which need main thread 
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
    
        }, delay);
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 1970-01-01
      • 2013-03-30
      • 2023-03-13
      • 2016-07-08
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      相关资源
      最近更新 更多