giovedì 28 febbraio 2013

Timer in Android

Creare una semplice applicazione di orologio puo' sembrare una cosa banale ma non in Android.
Per gestire il timer infatti e' necessario a ricorrere ad un thread separato che aggiorni l'interfaccia



Nell'esempio viene aggiornata una textview con l'ora corrente ogni 1000 millisecondi (1 sec)
-----------------------------------------------------------

package com.example.timer;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.widget.TextView;

@SuppressLint("SimpleDateFormat")
public class MainActivity extends Activity {

private TextView txttimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txttimer = (TextView)findViewById(R.id.timer);

Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
   public void run() {
    runOnUiThread(new Runnable() {
@Override
public void run() {
String tempo = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy").format(new Date(System.currentTimeMillis()));
txttimer.setText(tempo);
}

});
}
       
        }, 0, 1000);
    }
    
}

Nessun commento:

Posta un commento

Physics informed neural network Fukuzono

Visto che puro ML non funziona per le serie tempo di cui mi sto occupando ed le regressioni basate su formule analitiche mostrano dei limiti...