我需要一个应该始终运行的服务,直到我的活动显式停止它,并且即使由于某些问题(START_STICKY
标志)而停止它也应该重新开始.这项服务应该持续做一些事情(每隔几秒钟)使用一个TimerTask
.我最终得到了以下代码.
public class SomeService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
TimerTask timerTask;
final Handler handler = new Handler();
Timer timer = new Timer();
@Override
public void onCreate() {
// code to execute when the service is first created
super.onCreate();
}
@Override
public void onDestroy() {
// code to execute when the service is shutting down
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startid) {
// code to execute when …
Run Code Online (Sandbox Code Playgroud) 以下是我的代码:
在MainActivity.class中
private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {
if(CheckedButtonId==R.id.radiobutton_on){
Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
alert_on = true;
display_alert();
}
else{
alert_on = false;
}
}
};
public void display_alert(){
int delay = 10000;
Timer timer =new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
startService(myIntent);
}
}, delay,10000);
}
Run Code Online (Sandbox Code Playgroud)
MyService.class
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(MainActivity.alert_on)
Toast.makeText(getApplicationContext(), "Alert is On", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "Alert …
Run Code Online (Sandbox Code Playgroud) 我正在寻找Java计时器示例,并在http://www.javaprogrammingforums.com/java-se-api-tutorials/883-how-use-timer-java.html找到了以下代码
但是如果您运行该示例,虽然它现在打印Timer停止...它不会返回到命令提示符.这至少是使用cmd.exe在我的Windows XP机器上发生的事情.
在这种情况下,为什么不将控制权返回给提示符?
import java.util.Timer;
import java.util.TimerTask;
public class TimerSample {
public static void main(String[] args) {
//1- Taking an instance of Timer class.
Timer timer = new Timer("Printer");
//2- Taking an instance of class contains your repeated method.
MyTask t = new MyTask();
//TimerTask is a class implements Runnable interface so
//You have to override run method with your certain code black
//Second Parameter is the specified the Starting Time for your timer in
//MilliSeconds or …
Run Code Online (Sandbox Code Playgroud) 我有一个名为的任务timer
:
timer.schedule(new task1(), 1000*minutes);
Run Code Online (Sandbox Code Playgroud)
任务:
class task1 extends TimerTask {
@Override
public void run()
{
try {
task();
} catch (SAXException ex) {
Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
} catch (URISyntaxException ex) {
Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(task1.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void task() throws SAXException, ParserConfigurationException, IOException, URISyntaxException, InterruptedException {
Pinner_xml t = new Pinner_xml();
t.xml(frame.t1.getText());
frame.output.append("task 1 …
Run Code Online (Sandbox Code Playgroud) 如何安排计时器每 10 号运行一次?
非常感谢您提前
我的实际方法:
@PostConstruct
public void postConstruct() throws Exception
{
final TimerTask task = new TimerTask()
{
@Override
public void run()
{
myprocess();
}
};
final Timer timer = new Timer();
final long delay = 0;
final long intevalPeriod = 20000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
intevalPeriod);
}
Run Code Online (Sandbox Code Playgroud) 我希望在一定的延迟后调用一个特定的方法.我尝试了不同的aproaches,如计时器,执行程序或处理程序.他们满足了他们应该做的事情,但有一点例外.延迟方法调用在番石榴的多图中进行了一些更改.在run
TimerTask 的方法中,输出就像我想要的那样.但是如果我打印出多边形图,run
那么更改将被撤消,我仍然拥有旧的多图值.但我需要更新的,因为我的数据保存在其中,我需要使用更新的值.
我的代码看起来像这样:
public class classTimer {
public static void main(String[] args) {
//some code, irrelevant for the task
new Timer().schedule(new TimerTask() {
@Override
public void run() {
dataMap = UndoManager.undoChanges(dataMap, a, hw);
// Point 1
}
}, delay);
// Point 2
}
}
Run Code Online (Sandbox Code Playgroud)
就像我之前说过的那样,打印dataMap
at Point1会给出正确的输出,在Point 2处旧的值,比如Method UndoChanges
更新的调用.实现了多重映射,更改保持一致,并且通常必须更改值,但这不是这里的情况.我在这里错过了什么?如果有人知道在延迟后调用方法的方法不同,我会很高兴听到它.
谢谢,非常感谢
我正在练习服务,但坚持开始.我希望每隔3秒从服务中出现一次祝酒词.
我在做什么
myservice.java
package com.example.servicedemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class myService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public myService() {
// TODO Auto-generated constructor stub
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service created", 3000).show();
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个单独的单元供我的主窗体调用,除了使用TTimer
.
基本上,该函数应该做的就是主窗体uDataReceived
调用BlinkRect(Gateway)
以rRectControl
单元形式处理,并且相应的矩形将在主窗体中闪烁。
以下是代码:
unit uRectControl;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, System.IOUtils, FMX.Graphics, FMX.Types, FMX.Objects;
var
Blinks: array [0 .. 2] of record Rectangle: TRectangle;
Timer: TTimer;
end;
type
TMyClass = Class(TObject)
private
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
public
procedure BlinkRect(Gateway: integer);
end;
procedure AssignRectangles;
implementation
uses uDataReceived;
// Error shows "Cannot resolve unit name 'uDataReceived'
{ TMyClass }
procedure AssignRectangles;
var
i: integer;
begin
Blinks[0].Rectangle := TC_Theft_Detection.rect1;
// Error shows …
Run Code Online (Sandbox Code Playgroud) 在Java中是否有办法在特定时间基于一组天运行特定方法?
比方说我有以下几天:
2015年11月13日至2015年11月15日
时间是上午11点到下午6点.
所以我想要一种方法只在那些时候运行.
我知道计时器任务,但我不认为有一种方法可以设置多天,只在那些时间?在这些时间之后,我希望它在后台停止或运行,并且在更新这些日期之前不做任何事情.
我应该提到由于项目规范,我不能使用Quartz.有没有其他方法或这是不可能的?
我有2个活动,i)MainActivity ii)FavoriteActivity和非活动类RadioService.两个Activity都有一个相同的方法 - notifyShowBar(),它显示了一个在showbar.xml@layout中定义的文本栏.
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onResume() {
super.onResume();
notifyShowBar();
}
public void notifyShowBar() {
....
textView.setText(revs.getMeta());
....
}
.....
Run Code Online (Sandbox Code Playgroud)
FavoriteActivity.java
public class FavoriteActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onResume() {
super.onResume();
notifyShowBar();
}
public void notifyShowBar() {
....
textView.setText(revs.getMeta());
....
}
.....
Run Code Online (Sandbox Code Playgroud)
现在,当一个活动调用RadioService时,它会创建一个timertask,然后定期从服务器获取信息,并在父活动中显示bar.因此栏显示每15秒更新一次信息.
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPostExecute(final Boolean …
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我正在尝试创建一个服务来连接到我创建的API.
此服务必须每小时连接到Mysql中的数据库,并查看是否存在某个值.
例如,每当我看到字段x的值是否为y时.如果是真的,就必须运行一些东西.
我已经阅读了一些关于Threads和System.Threading.Timer的内容,但是不太明白,有人可以给我一些实际的例子或者正确的方法,我正在寻找什么,请?
提前致谢 ..
timertask ×11
java ×5
timer ×5
android ×4
service ×2
c# ×1
delphi ×1
delphi-xe7 ×1
firemonkey ×1
guava ×1
multimap ×1
swing ×1
timer-jobs ×1