I decided to ease the update time algorithm. Everywhere write an algorithm through the Timer. But if you implement Runnable the interface in the main activity, you can avoid creating an extra object.
I want it when I open the activation, the time was updated every second. But this does not happen. By the tag (ITTERATION) that I created, there is only one iteration. Using an example from HERE
I already broke my brain. Help me please :(
package pac.twoproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class Main2Activity extends AppCompatActivity implements Runnable {
private static final String TAG = "ITTERATION";
TextView tv;
String time;
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv = (TextView) findViewById(R.id.textView);
//scheduler.scheduleAtFixedRate(this, 0, 10, TimeUnit.MILLISECONDS);
}
@Override
protected void onResume() {
super.onResume();
scheduler.scheduleAtFixedRate(this, 0, 1000, TimeUnit.MILLISECONDS);
}
@Override
public void run() {
time = sdf.format(new Date(System.currentTimeMillis()));
tv.setText(time);
Log.d(TAG, time);
}
}