相关疑难解决方法(0)

如何在Android延迟后调用方法

我希望能够在指定的延迟后调用以下方法.在目标c中有类似的东西:

[self performSelector:@selector(DoSomething) withObject:nil afterDelay:5];
Run Code Online (Sandbox Code Playgroud)

android中有没有与java相同的方法?例如,我需要能够在5秒后调用方法.

public void DoSomething()
{
     //do something here
}
Run Code Online (Sandbox Code Playgroud)

java android delay handler

716
推荐指数
19
解决办法
60万
查看次数

现在 Handler() 已被弃用,我该使用什么?

如何修复此代码中的弃用警告?或者,还有其他选择吗?

Handler().postDelayed({
    context?.let {
        //code
    }
}, 3000)
Run Code Online (Sandbox Code Playgroud)

java android kotlin android-handler

184
推荐指数
12
解决办法
7万
查看次数

Timertask或Handler

假设我想每10秒执行一次操作,并不一定需要更新视图.

问题是:使用带有timertask的计时器是否更好(我的意思是更有效率和更有效),如下所示:

final Handler handler = new Handler();

TimerTask timertask = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            public void run() {
               <some task>
            }
        });
    }
};
timer = new Timer();
timer.schedule(timertask, 0, 15000);
}
Run Code Online (Sandbox Code Playgroud)

或者只是一个带有postdelayed的处理程序

final Handler handler = new Handler(); 
final Runnable r = new Runnable()
{
    public void run() 
    {
        <some task>
    }
};
handler.postDelayed(r, 15000);
Run Code Online (Sandbox Code Playgroud)

如果您能解释何时使用哪种方法以及为什么其中一种方法比另一种方法更有效(如果它实际上是这样),我将不胜感激.

performance android timer handler timertask

96
推荐指数
3
解决办法
6万
查看次数

kotlin coroutines,coroutineScope和withContext有什么区别

withContext
suspend fun <T> withContext(
    context: CoroutineContext, 
    block: suspend CoroutineScope.() -> T
): T (source)
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result.
Run Code Online (Sandbox Code Playgroud)
suspend fun <R> coroutineScope(
    block: suspend CoroutineScope.() -> R
): R (source)
Creates a CoroutineScope and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, but overrides the context’s Job.
Run Code Online (Sandbox Code Playgroud)

withContext采用CoroutineContext,并且似乎都complete在其所有子级完成之后。

在什么情况下,withContext或者coroutineScope应该比其他的首选? …

kotlin-coroutines coroutinescope withcontext

8
推荐指数
1
解决办法
395
查看次数

Kotlin:每秒调用一个函数

我想为我的游戏创建一个简单的倒计时,当游戏开始时,我希望每秒调用一次此函数:

fun minusOneSecond(){
  if secondsLeft > 0{
     secondsLeft -= 1
     seconds_thegame.text = secondsLeft.toString()
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了这个:

var secondsLeft = 15

timer.scheduleAtFixedRate(
   object : TimerTask() {

      override fun run() {
         minusOneSecond()
      }

    },0, 1000
)   // 1000 Millisecond  = 1 second
Run Code Online (Sandbox Code Playgroud)

但不幸的是,该应用程序停止了,第二次调用run函数

3周前,我才刚开始进行android开发和Kotlin,到目前为止,我对它的了解最多。

通过Xcode的快速操作,我使用了这一行,我认为类似的方法也可以在Kotlin中使用

setTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(minusOneSecond), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

android kotlin

6
推荐指数
4
解决办法
5574
查看次数