在服务Android中每x次运行一次功能

时间:2014-03-19 17:05:46

标签: android android-service scheduledexecutorservice

我正在开发一个应用程序,但我需要运行一个函数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 );
}

但它不起作用。  我在服务上使用此功能,当服务运行时,它不会调用该函数。

2 个答案:

答案 0 :(得分:0)

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

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.
 }
} 
}

答案 1 :(得分:0)

请参阅此链接:

http://developer.android.com/reference/android/os/CountDownTimer.html

这可能对您有所帮助