我正在开发一个Android 2.3.3应用程序,我需要每隔X秒运行一个方法.
在iOS中,我有NSTimer,但在Android中我不知道该使用什么.
有人推荐我Handler ; 另一个推荐我AlarmManager,但我不知道哪种方法更适合NSTimer.
这是我想在Android中实现的代码:
timer2 = [
NSTimer scheduledTimerWithTimeInterval:(1.0f/20.0f)
target:self
selector:@selector(loopTask)
userInfo:nil
repeats:YES
];
timer1 = [
NSTimer scheduledTimerWithTimeInterval:(1.0f/4.0f)
target:self
selector:@selector(isFree)
userInfo:nil
repeats:YES
];
Run Code Online (Sandbox Code Playgroud)
我需要一些像NSTimer一样的东西.
你推荐我什么?
首先,我甚至无法选择使用该方法,我现在正在阅读几个小时,有人说使用'处理程序',有人说使用'计时器'.这是我试图实现的目标:
在首选项中,有一个设置(复选框),用于启用/禁用重复作业.选中该复选框后,计时器应该开始工作,并且每隔x秒执行一次线程.由于未选中复选框,计时器应该停止.
这是我的代码:
检查是否选中了复选框,如果选中'refreshAllServers'void将执行,它将执行带有计时器的作业.
boolean CheckboxPreference = prefs.getBoolean("checkboxPref", true);
if(CheckboxPreference == true) {
Main main = new Main();
main.refreshAllServers("start");
} else {
Main main = new Main();
main.refreshAllServers("stop");
}
Run Code Online (Sandbox Code Playgroud)
执行计时器作业的refreshAllServers void:
public void refreshAllServers(String start) {
if(start == "start") {
// Start the timer which will repeatingly execute the thread
} else {
// stop the timer
}
Run Code Online (Sandbox Code Playgroud)
这就是我执行我的线程的方式:(没有计时器就可以正常工作)
Thread myThread = new MyThread(-5);
myThread.start();
Run Code Online (Sandbox Code Playgroud)
我尝试了什么?
我试过任何我可以从谷歌(处理程序,计时器)看到的例子,没有一个工作,我设法启动计时器一次,但停止它不起作用.我在研究中看到的最简单易懂的代码是:
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
// your code …Run Code Online (Sandbox Code Playgroud) 我有以下代码用于每隔X秒从服务器轮询未读通知计数
我通过App.onCreate()中的ScheduledThreadPoolExecutor启动此过程,然后
Log.d("XXX", "Requesting Notification count from server ...");
Run Code Online (Sandbox Code Playgroud)
一次被调用(我在Logcat中可以看到),但是两个Retrofit回调函数都没有被调用(实际上没有Retrofit调试日志)。此外,永远不会再次打印“正在从服务器请求通知计数...。”(即,定期任务未运行)
我也将Retrofit用于其他Web服务调用(根据用户输入),并且它们工作正常(我可以在logcat中看到传入和传出的请求/响应)
public class App extends Application {
private ScheduledExecutorService scheduleTaskExecutor;
...
@Override
public void onCreate() {
super.onCreate();
//region Set up the periodic notification count listener task
scheduleTaskExecutor= Executors.newScheduledThreadPool(2);
scheduleTaskExecutor.scheduleAtFixedRate(new PeriodicNotifCountFetchTask(), 0, 5, TimeUnit.SECONDS);
//endregion
}
class PeriodicNotifCountFetchTask implements Runnable {
@Override
public void run() {
Log.d("XXX", "Requesting Notification count from server ...");
EMRestClient.getmEMRestService().getNotificationCount(new Callback<NotificationCount>() {
@Override
public void success(NotificationCount response, Response unused) {
int unreadNotifCount = response.getCount();
Log.d("XXX", "Successfully fetched …Run Code Online (Sandbox Code Playgroud) multithreading android android-asynctask scheduledexecutorservice retrofit
我使用ScheduledExecutorService,我希望它每隔10秒进行一次计算一分钟,然后在那一分钟后给我返回新的值.我该怎么做?
示例:所以它收到5它增加+1六次然后它应该在一分钟后返回值11.
到目前为止,我没有工作的是:
package com.example.TaxiCabs;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.*;
public class WorkingWithTimeActivity {
public int myNr;
public WorkingWithTimeActivity(int nr){
myNr = nr;
}
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public int doMathForAMinute() {
final Runnable math = new Runnable() {
public void run() {
myNr++;
}
};
final ScheduledFuture<?> mathHandle =
scheduler.scheduleAtFixedRate(math, 10, 10, SECONDS);
scheduler.schedule(
new Runnable() {
public void run() {
mathHandle.cancel(true);
}
}, 60, SECONDS);
return myNr;
}
Run Code Online (Sandbox Code Playgroud)
}
并在我的主要活动中,我希望它在1分钟后将我的txtview文本更改为11;
WorkingWithTimeActivity test …Run Code Online (Sandbox Code Playgroud)