How To Calculate Android App Runtime ~Android - java

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.

Related

How to send a local repeating notification in certain days?

I have a reminders app, I made a UI that looks like this where the user can pick multiple days and the hour that he wants to be reminded on. For example, I can select Monday and Tuesday at 15:00 and I should be reminded those days at that given hour. But I canĀ“t figure out how to make this work (mainly, the mutiple days thing). Any ideas?
You can send a local repeating notification with the help of AlarmManager. setRepeating() function can be used to schedule notifications.
similar question, Check this link for better understanding.

Android show information in a specific day on calendar

Not a problem per se but I am figuring out how to do something. In my app, I shall save some workout for every specific client. Any workout will be doing different from week to week; i.e: rep scheme in the week one shall be different from the week two, and so on.
So, how to show the user that in the calendar? Should I save in every workout the day it should be performed and then see if it's the same day and show it? Or how to implement this? The only way I thought to implement this is save in every workout the day that it should be performed and show it in a textview and all the info behind relationated with the workout without calendar use but don't know if this approach would be the best
I have searched on the web but found nothing. If you could give me a hint would be awesome.
Thanks in advance!

Android Date: Determining Future Date From Today

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.

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

How to get current time on another city correctly?

I have managed to read the web service to get current time of any given city.
I could get 2 important values from web service, current time (String) and the offset.
Question is
How to set time of any given city correctly?
Option 1:
Read machine/local time
Calculate UTC/GMT time out of machine time
City time = UTC time +/- offset value
But then what happens when machine time is wrong? You will also got
wrong time right?
Option 2:
Read current city time in String (2012-11-24 19:30)
Parse this time value and set it into Calendar
We got correct City time
But how about the next minute? Of course requesting the web service every minute to get current time is not a good solution right? Is it possible to maintain this Calendar instance keep running automatically every minute once we set it?
NB : I'm developing Android clock widget here.
Thanks
Option 1 is far better, in my eyes. Most cell phones have amazingly accurate time as time synchronization is an integral part of GSM and CDMA. Beyond that, I would far prefer a clock to work offline than to require internet connectivity.
If you are worried about ensuring accuracy in the face of incorrect system time, consider placing a call to a web service to get the current time for verification.
This verification could be done in the background, but keep in mind that web services are not the best time sync providers. I would let anything with under 5 minute difference go as it could be due to your server being out of sync or the call taking too long.

Categories