【问题标题】:run function every x time in service Android在服务 Android 中每 x 次运行一次函数
【发布时间】:2014-03-19 17:05:45
【问题描述】:

我正在开发一个应用程序,但我需要运行一个函数 x 分钟,我已经尝试过使用

scheduler = Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(new Runnable() {
        @Override public void run() {
            Toast.makeText(ser, "Servicio MyService update", Toast.LENGTH_LONG).show();
        }
    },
            0,
            100,
            TimeUnit.MILLISECONDS );
}

但它不起作用。 我在服务上使用这个函数,当服务运行时它不会调用这个函数。

【问题讨论】:

    标签: android android-service scheduledexecutorservice


    【解决方案1】:

    如果我理解错了,请纠正我, 你可以使用递归函数,

    public void myfunc()
    {
    new CountDownTimer(70000, 1000) {
    
     public void onTick(long millisUntilFinished) {
         //
     }
    
     public void onFinish() {
         Toast.makeText(getApplicationContext(), "7 seconds have passed",
       Toast.LENGTH_LONG).show();//you can do here what you want every x(7)time
    //you can put condition here to break recursive
        myfunc();   //this calls again.
     }
    } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2023-04-08
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多