Timer - to count Time - android

I am making a quiz application, in which I want to show the time used by the user while playing a session of game. It should be in format HH:MM:SS starting from 00:00:00 till he selects the answer. Timer should be updating every second while user is playing each second. Also, I want to pause the timer while next question is loading. I am not able to understand how to do this. Right now I am able to fetch current time by this code.
Calendar c = Calendar.getInstance();
SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
final String startTime = timeformat.format(c.getTime());
Play.timer_box.setText(""+startTime);
It just display whatever the current time is in the box in right format. But not able to understand how to achieve what i want to

You cant just show the time using calendar,you have to write the logic
Start counter when activity/fragment is resumed till user selects
answer
Stop it once user selects the ans,ans start it again once next
question (activity/fragment is loaded)
Convert counter to the time format you want to display,you will need
a logic for the same

Related

I want to schedule a background task that runs every minute and check a condition until condition is True in Android

I am designing a birthday based Android app. This app check current system date, if date is birth date, app will start a music and allow other activities. Else, the app will give an error message to wait until birth date and exit.
Below is the sample code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateobj = new Date();
Date date1 = sdf.parse(sdf.format(dateobj));
Date date2 = sdf.parse("2016-05-30");
if (date1.compareTo(date2) >= 0) {
// Start a music
//Other task
}
else
{
// You need to wait message
}
Now, I want this condition to be checked after every 60 seconds (1 minute) automatically, even if app is not open [Probably alarmManager might help, but I am not sure how] , something like a background task. This task should be repeated until condition is true.
If user opens the app before the birth date, an error alert is displayed and app is closed.
You should use the AlarmManager setRepeating function to achieve that. Check this link for details - https://developer.android.com/training/scheduling/alarms.html
As for your requirement of running the code even when it is not open, you should make use of BroadcastReceiver. You need to pass your BroadcastReceiver as a PendingIntent to the AlarmManager.

comparing input Date & Time with current date & time in android

I am creating a "To-do Things" app where a user can create tasks to be done. In the Main activity I have a fragment which consists of three EditTexts ie Notification Content , Date & Time of notification.
Each new entry is added to a database. This database is displayed in form of ListView in another activity.
My Questions are-:
Q1) I want to compare date & time of each entry & print only those which are about to come ie whose date & time is after the present date & time.So how should I perform the comparison?
Q2) When the date & time is reached generate a notification .
Q1) Use the Calendar class. You can take a Calendar object and set its date and time with your user data (using Calendar.set) and then compare with another calendar which you set to the current time as follows:
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
To compare two calendars you can use the Calendar.before method, or you can convert them back into "timeInMillis" and calculate the difference in milliseconds.
Q2) Use theAlarmManager and a BroadCastReceiver

Dateformat not updated when the time is changed

i am developing an android app where in i am displaying the date and time for the user on the main screen of my app. I am using the below code for it.
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy");
String strtDates = sdf.format(cal.getTime());
textviewfordate.setText(strtDates);
clk = (DigitalClock)findViewById(R.id.digitalClock);
However, when the time is changed, the date doesnot get updated without refreshing the screen. For example when the time is 11:59:00 PM and when the time changes to 12:00:00 AM, the date will not be updated. It gets updated only when the screen is refreshed. I want the date to be changed automatically without refreshing the screen.
Not getting how to do it! Please Help! Thanks!
If you set a text to textview in onCreate(), it will run only once when that method is called and won't get updated everytime the time changes.
For your need, you have to use handler or executor or any other thread which runs for every user defined time, and you have to set the current time text to textview inside them. So, that the text will be updated.
And also refer the following links, they will help you:
Android run thread in service every X seconds
How to execute a function every two seconds in Java on Android?
display data after every 10 seconds in Android

Retrieve selected time from TimePicker

I have a database with a table which contains a start time and an end time. The user has to select both through the Android app, and then I would store the two times in my database.
For now, I have 2 TimePicker in my xml file, and I have 2 TimePicker in my java file TimePicker start_time = (TimePicker) findViewById(R.id.timePickerStart);
I tried to create a Time : Time start = new Time(start_time.getCurrentHour(), start_time.getCurrentMinute(), 0); but this method happens to be deprecated. Do you know another way to do it?
My purpose is to send a notification (Toast?) to the user at those times. Do you know how can I link the current time with the time entered by the user?
I'm sorry for asking so many questions, but I'm really lost here :(
Use Date class instead of Time.
Even better use Calendar. Most methods of Date are deprecated.
Calendar calendar = new GregorianCalendar(0, 0, 0, hour, minute, 0);

Calling the date time , without initiating the dialog box of Datetime control

I am basically using a datetime control in my application.
Wherein on a click event i am initiating datetime dialog.
In another scenario, i want to move current date, to next day, or previous day.
I don't want the dialogbox of date time control to be displayed.
Is it possible??
The DatePicker and TimePicker controls are available as widgets for you to use in your layouts or code.
You can then build the next day / previous day functionality yourself by setting the date / time of those controls.

Categories

Resources