Chcialbym zeby funkcja odpowiedzialna za pobieranie danych o pliku robila to cyklicznie co 30 sekund.. tak aby wyswietlacz pokazywal ciagle aktualna zawartosc..
jjak to zrobic? z gory dzieki
aktualny kod nizej
Chcialbym zeby funkcja odpowiedzialna za pobieranie danych o pliku robila to cyklicznie co 30 sekund.. tak aby wyswietlacz pokazywal ciagle aktualna zawartosc..
jjak to zrobic? z gory dzieki
aktualny kod nizej
Nie wystarczy jakis Timer ktory wywolywałby metode pobierajaca plik co okreslony czas?
Dodatkowo na pewno przyda się:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html
dzieki za odpowiedz :)
mam pytanie co do tego timera bo rzeczywiscie pobiera on dane co sekunde ale dopiero po tym jak wykonam jakas akce na komorce... bo na samym poczatku dane z pliku zostaja zaladowane wstawione i nie zmieniaja sie... po wykonaniu akcji jak np przejscie strzalkami na jakis element form-a timer jakby sie uruchamia i rzeczywiscie zmiany w pliku na serwerze sa na biezaco aktualizowane na wyswietlaczu.. Cel jest oczywisty: zeby od poczatku uruchomienia aplikacji timer spelnial swoje zadanie i od razu aktualizowal co sekunde dane
oto kod programu:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package futbol24;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SimpleTableModel;
import org.netbeans.microedition.lcdui.TableItem;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author bart
*/
public class main extends MIDlet {
private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
private Form screen1;
private DateField dateField;
private StringItem stringItem;
private TableItem tableItem;
private SimpleTableModel tableModel1;
//</editor-fold>
/**
* The main constructor.
*/
public main() {
}
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
/**
* Initilizes the application.
* It is called only once when the MIDlet is started. The method is called before the <[code]>startMIDlet</[code]> method.
*/
private void initialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {
// write pre-action user code here
switchDisplayable(null, getScreen1());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {
// write pre-action user code here
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
/**
* Switches a current displayable in a display. The <[code]>display</[code]> instance is taken from <[code]>getDisplay</[code]> method. This method is used by all actions in the design for switching displayable.
* @param alert the Alert which is temporarily set to the display; if <[code]>null</[code]>, then <[code]>nextDisplayable</[code]> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: screen1 ">
/**
* Returns an initiliazed instance of screen1 component.
* @return the initialized component instance
*/
public Form getScreen1() {
if (screen1 == null) {
// write pre-init user code here
screen1 = new Form("form", new Item[] { getDateField(), getStringItem(), getTableItem() });
// write post-init user code here
}
return screen1;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: dateField ">
/**
* Returns an initiliazed instance of dateField component.
* @return the initialized component instance
*/
public DateField getDateField() {
if (dateField == null) {
// write pre-init user code here
dateField = new DateField("dateField", DateField.DATE_TIME);
dateField.setDate(new java.util.Date(System.currentTimeMillis()));
// write post-init user code here
}
return dateField;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">
/**
* Returns an initiliazed instance of stringItem component.
* @return the initialized component instance
*/
public StringItem getStringItem() {
if (stringItem == null) {
// write pre-init user code here
stringItem = new StringItem("stringItem", "rjwejrlkwer");
// write post-init user code here
}
return stringItem;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableItem ">
/**
* Returns an initiliazed instance of tableItem component.
* @return the initialized component instance
*/
public TableItem getTableItem() {
if (tableItem == null) {
// write pre-init user code here
tableItem = new TableItem(getDisplay(), "tableItem");
tableItem.setModel(getTableModel1());
// write post-init user code here
}
return tableItem;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableModel1 ">
/**
* Returns an initiliazed instance of tableModel1 component.
* @return the initialized component instance
*/
public SimpleTableModel getTableModel1() {
if (tableModel1 == null) {
// write pre-init user code here
tableModel1 = new SimpleTableModel(new java.lang.String[][] {
new java.lang.String[] { "" }}, null);
String[][] tString = new String[5][0];
DataTransfer dt = null;
dt = new DataTransfer();
dt.httpAddress = "http://kolos.math.uni.lodz.pl/~bart/tekstowy.txt";
try {
tString = dt.get_TextBox();
} catch (IOException ex) {
ex.printStackTrace();
}
tableModel1.setValues(tString);
// write post-init user code here
}
return tableModel1;
}
//</editor-fold>
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
Timer t = new Timer();
TimerTask tt =new TimerTask(){
int count=0;
public void run() {
String[][] tString = new String[5][0];
DataTransfer dt = null;
dt = new DataTransfer();
dt.httpAddress = "http://kolos.math.uni.lodz.pl/~bart/tekstowy.txt";
try {
tString = dt.get_TextBox();
} catch (IOException ex) {
ex.printStackTrace();
}
tableModel1.setValues(tString);
}
};
// wykonywanie co sekunde
t.schedule(tt,0, 1000);
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
}
startApp() wykonuje sie raz podczas uruchomienia midletu, oraz kiedy nastepuje "odpausowanie" programu.
Ty inicjujesz timer w startApp() cyzli nie jest on widonczny poza ta metodą.
Pewny nie jestem, ale sprobuj ten timerek wstawić do konstruktora, albo stworzyc go gdzies poza startApp().
niestety timer w konstruktorze tez dopiero sie odpala po wykonaniu akcji co mnie dziwi bo zgadzam sie z tym co piszesz i teraz to jest troche nielogiczne... moze jakies pomysly?
Hmm, niemoge tego sprawdzic na twoim kodzie, bo nie mam bibliotek ktorych uzywasz. Ale zrobiłem prosty midlet i niezaleznie czy zawartosc startApp() bedzie w startApp() czy w konstruktorze to i tak timer dziala tak jak powinien.
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet {
Timer t;
Timertask tt;
public Midlet(){
}
public void startApp() {
t = new Timer();
tt = new Timertask();
t.schedule(tt,0,1000);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
class Timertask extends TimerTask{
public void run() {
System.out.println("Timerek");
}
}
}