Android Date: Determining Future Date From Today - java

I have an app that allows users to set reoccurring alarms. In a fragment, they're allowed to pick a time from a TimePicker and set switches for what day they want the alarm to occur i.e. Mon,Wed,Fri.
I'm saving the time from the TimePicker in milliseconds and the days they choose as a string in an Alarm object like below.
public class Alarm extends RealmObject{
#PrimaryKey
private int id;
private Long time;
private String title;
private String dayOfWeek;}
What I'm trying to determine is how I figure out the future dates based on the days they want to schedule. For example, if today is Monday and they want to schedule an alarm for Mon, Wed and Fri, I would save an alarm object with a time of today, another alarm object with a time of today + 2 days and lastly one with an alarm of + 4 days.
However, the issue is this changes based on what day today is. If today is Tuesday, then I'll need to schedule an alarm for + 1 day, +3 days and + 6 days. What's the easiest way to calculate this day offset?

If I understand your question correctly, I would consider adding user shared preference (http://developer.android.com/reference/android/content/SharedPreferences.html)
containing a string alarmDaysOfWeek="Mon,Wed,Fri" or whatever value is based on user input.
Then you'd implement a Service that checks current day of week against the alarmDaysOfWeek in user's SharedPreferences and alarms if it's a match.

Just a suggestion:-
Set an alarm for everyday and when an Alarm Occur(Service,BroadcastReceiver) then check it if you have to something today(as par user's selection) or just do nothing.
By above ways you wont need calculations for +1 or +2.

I was able resolve this by capturing what day the user selected and then setting the day of the week on the calendar object like newAlarm.setDayOfWeek(x.getDayOfWeek) and then setting the time of the alarm object based on that. This way of a user selected a time in the past i.e. if today were Tuesday and they set an alarm for Monday, it would actually set the calendar object time in the past. Then when the alarm was scheduled, it would immediately fire and I could reschedule it for +7 days or next Monday.

Related

Create alarm using AlarmManager and BroadcastReciver

How to create an alarm using AlarmManager and BroadcastReciver to allow scheduling alarm within several days of the week (e.g. Sunday, Monday, Wednesday and Friday) ?
Thanks!
Please follow these links below describe will solve your problem:
how can i get the repeat alarm for week days using alarm manager in android?
https://www.journaldev.com/27681/android-alarmmanager-broadcast-receiver-and-service

Android Alarm Scheduling with repeats

I am in need of creating an app with "reminding" facility. For an example, user can set a reminder for "Jogging" and he can do following when setting up this reminder. Below is the normal process.
User setup the reminder with "cause" to be reminded (ex: jogging )
User can set the reminder to fire at 10am Monday, and so on.
If required, user can set the alarm for multiple days (ex: Monday, Tuesday, Friday )
user can set the reminders to be repeated. (Ex: "Every Monday at 10am". Ie else "Every Monday, Tuesday, Sunday at 10am ")
User can set any number of reminders for various causes.
At the time of reminder, the user will get a notification in his phone.
After setting the reminder, entire process will run in background.
I was trying to find a better way to do this, this is basically like an alarm application. I looked into 'AlarmManager' , 'JobScheduler' and so on, nothing seems to match what I want to do, the correct alarm set-up and repeated reminders when set.
I looked into some alarm app examples too, seems too complicated when my core app is not an "alarm" app.
What is the exact library of doing this? I am not creating an alarm app, this is a process required for another app we build.

Highcharts Scatter, plotting 3 days data on 12AM to 12PM of one day x-axis

I have data of 3 days and we have following requirement.
1- X-Axis must be 12AM to 12PM with 2 hour interval.
2- All days data should be plotted on time between 12AM to 12PM.
3- I am not going to show 3 days chart with interval of 2 hours with 3 different slots like 12AM...... 12PM of one day, 12AM........12PM of 2nd day, and 12AM.........12PM of 3rd day.
How can i plot different days data in scatter chart?
you can see in below image.
above image contains 3 days data with the technique given below,
firstly i get the data from database and extract all dates, and added first date of range in the date timestamp, and my x-axis was just start day time of range's first day and last day time of range's first day.
*keep in mind localization is implemented as well. (data is populating as per client timezone)
client/ User = GMT +5
Server = UTC
Now problem is that with this solution is, User selected date range 1-sep-2015 to 3-sep-2015, and user logged the data on 12AM of 1st sept 2015 GMT +5, it saved in DB UTC 7PM of 31 aug 2015, this data is not being plot on the chart because it go outside the range because x-axis is start day time of 1st sep 2015 to end start day time of 2nd sep 2015.
Now can you suggest any solution of this ?
You seem to have the plotting itself down; this is mostly a data retrieval question as I understand it.
So take the time range you want in local time (Sep-01 00:00 to Sep-03 23:59) and convert it to UTC (Aug-31 19:00 to Sep-03 18:59). Run your query for events in that time range, and then for each event you get back, convert it back to local time.
You may be able to do all the timezone conversions inside the database itself, depending on what database you are using; if not, doing it in the business logic is not a big deal at all with the dataset size you seem to be working with.

How To Calculate Android App Runtime ~Android

I need to calculate my Android App runtime in a day. Is there a way to do this?
For example:
If I start(open) my app at Sunday 07:00AM, and I close my app at Sunday 07:15AM, it will get 15 minutes of runtime. And If I open again my app at Sunday 11:50PM and I close my App at Monday 00:20AM in the very next day, It will get 25 minutes of runtime on Sunday and 20 minutes of runtime on Monday.
Is there a practical way to accomplish this?
Thanks in advance...
I would solve this by saving the current dateTime to your sharedpreferences in your onResume.
In your onPause I would get the saved startTime from your preferences and calculate the difference with the current time. Next, clear the saved starttime and update your database with the time the user was active this day.

How to get the "last" date or time that the user has run the application in android?

How to get the time or a date of a user turning off an application?
um
Clash of Clans for example, you can see your clan users' last visiting time or last turning off time of the application.
such as "5 minutes ago", or "1 day, 3 hours ago"
i just want to know the specific method name of this and look for it at google android references.
There is no specific method for this, you can get the systems current time and save it in a shared preference.
To get the time and date use:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
To save this time in SharedPreference:
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putString("NameOfThingToSave", currentDateandTime);
editor.commit();
Beware:
This technique largely depends on your system time, if a user changes the time/date on the device your logic will get messed up. The proper way to do this would be to sync the time with your server time!
Most importantly you should use your activities life-cycle methods to determine when the activity starts (OnCreate/onStart) ann when your activity stops (onStop)
Maybe you can save the current time in your onStop() and when you open the application again display the time saved when your app was closed.
You can store all the informations you want using SharedPreferences

Categories