Answer the question
In order to leave comments, you need to log in
timer in android?
Hello everyone, I ran into another problem, although it seemed that it could go wrong here ..
The fact is that I don’t know how to create a standard timer without creating a new class, that is:
in onCreate I created a timer to execute a method in the same class, that is activity
In this method, a timer will be created again (you need to count 8 seconds and decrease the progress bar every second)
I heard about CountDownTimer (Countdown Timer), but I can’t find examples ..
Please tell me how to release?
Answer the question
In order to leave comments, you need to log in
what's wrong with the class? How much I understood from the timer it is necessary to cause the timer? Just in case, I threw an example of a "pomodoro" timer (I did it recently), maybe it will help ...
public class MainActivity extends AppCompatActivity {
WorkClass timer;
RelaxClass r_timer;
FloatingActionButton play;
FloatingActionButton stop;
TextView display;
CircularProgressBar circularProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
display = (TextView)findViewById(R.id.time);
display.setText("02:00");
circularProgressBar = (CircularProgressBar)findViewById(R.id.progress);
stop = (FloatingActionButton) findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timer.cancel();
play.setVisibility(View.VISIBLE);
stop.setVisibility(View.INVISIBLE);
}
});
play = (FloatingActionButton) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stop.setVisibility(View.VISIBLE);
play.setVisibility(View.INVISIBLE);
int animationDuration = 60000;
circularProgressBar.setProgressWithAnimation(100, animationDuration);// Default duration = 1500ms
timer = new WorkClass(120000, 500);
timer.start();
}
});
}
public class WorkClass extends CountDownTimer {
public WorkClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
display.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
}
@Override
public void onFinish() {
if (display.getText().equals("00:00")) {
display.setText("01:00");
showNotify("Устал?", "Тогда удаляй приложение");
r_timer = new RelaxClass(60000, 500);
r_timer.start();
} else {
display.setText("2:00");
}
}
}
public class RelaxClass extends CountDownTimer {
public RelaxClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
display.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
}
@Override
public void onFinish() {
if (display.getText().equals("00:00")) {
display.setText("02:00");
showNotify("Отдохнул?", "Настал час расплаты");
timer = new WorkClass(120000, 500);
timer.start();
r_timer.cancel();
} else {
display.setText("2:00");
}
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotify(String title, String note) {
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification mNotification = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(note)
.setSmallIcon(R.drawable.playbutton)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question