How do I save chronometer elapsed time in android app - java

I'm developing a sort of parking meter counter app for a few specific locations. The app asks the user where he is (dropdown menu, saves column letter and number, like A6) and then shows a screen with the given location, a timer (chronometer class) counting up from 0 and the price that has to be paid on exit (calculated using elapsed hours * base price).
This all works nicely, until the user or the OS kills the app (task manager or memory management). Then, the next time the user opens the app, he's back at the main menu and his location, time and price has been lost.
I need a way to save all of the user's information and be able to load it up on app restart.
Initially I had thought to save the user's location and the exact time the chronometer started (DateTime.Now() maybe?) to a .txt file in the internal storage so that it would read:
mallname,columnLetter,columnNumber,startTime
This way, if the app is killed, or any time the app is started up from scratch, the MainActivity will first check if a file.txt exists in the app's internal storage, if it does, it immediately starts the lastPage activity, reading the .txt file to pass the values as parameters (thus, the comma separation). If there's no such file, it would just carry on normally. The last page does contain a reset button that would delete the file so that the app can start up normally the next time it's used.
I don't know how efficient this method is, but it's the first thing that came to mind. However, I don't know how to go about this. I have this same exact method programmed in C# for Windows Phone, but I don't know how to translate it into Java for Android. Also, in C# I didn't actually use a chronometer, rather, I had the startTime saved and used a timer that would calculate startTime - DateTime.now() on every tick (every second) and update the textBlock to show this.
Any help would be greatly appreciated.

Usually user data should be saved to a SQLite database, however in your case, since you're talking about a few variables, it'd be much easier for you to save these values in the application's SharedPreferences.
For every value you'd like to save, add this line to your Chronometer's onChronometerTick() method:
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("your_key", <Your_value>).commit();
This can later (whenever your application recovers from a crash, for example) be retrieved by:
PreferenceManager.getDefaultSharedPreferences(this).getInt("your_key", <Default_value>);
Regarding performance issues, if your Chronometer ticks once every second this should have no effect on your app's performances and is completely acceptable.

Related

Firebase Jobdispatcher - to use or not to use

I am developing an App, a simple, but hopefully addictive little game. The user has to solve predefined levels, as quick as possible.
Information on the levels is stored online in an MySQL database, which also contains the average time it took all players to complete a given level. Also, the level-data is stored, locally, in a SQlite database on the phone.
What I want to do is the following. I want to synchronize the average time (from server to phone) and upload the time it took a player to complete a leve (from phone to server).
Ideally this happens each time the player starts the app or finishes a level. For this, I am considering a Firebase Jobdispatcher, but I was wondering if this is overkill or not. For your information: it is not the end of the world if the average time stored on the phone is not entirely up to date. The game will work just fine without it being up to date. On the other hand, I want it to get updated regularly as the performance of the user will be compared to the average time.
I am a beginner, who wants to do things correctly. Hope you can help.
It sounds like you already know when some work should happen. As you said:
Ideally this happens each time the player starts the app or finishes a level.
You don't need JobDispatcher to schedule work when you are already in control of the times when the work should happen. JobDispatcher is used when you need to schedule some work at some point in time or interval when your app may not even be running.

Saving real time data in Android application to access later

I am currently streaming information in the form of a String from an Arduino to an Android application via Bluetooth. My application currently just displays the data on a series of progress bars as the values come in. I would like to store the data as it comes in to be viewed later but am unsure as to how to achieve this.
I have a BluetoothActionListener which runs when new data is available. This is called up to 20 times per second so I am worried as to whether adding a memory save to a file within this function will have an effect on its performance.
The data arrives as a string via bluetooth and I would like to be able to store the entire session's worth of data (upto 10mins) in the same file on a different line. A simple text file with a timestamp on each line is sufficient.
Would using a internal/external memory write cause my foreground visuals to slow down or even become unresponsive, as each new string arriving causes each of the progress bars to update?
At twenty times a second? Probably not. Just keep the file open rather than closing it, and flush it every few writes.

How to get correct universal timestamp in java if the time is set wrong on the computer?

Is there any way to get the results like in this link having wrong date and time set on device? Thank you.
There's a way1 ...
Suppose that you have deliberately set the clock 1 minute slow.
Create a file that contains the number 60,000. When an application wants to find the correct time, it calls System.getTimeMillis() to get what the system thinks the time is. Then reads the number from the file and adds that number to the result of System.getTimeMillis().
Clearly, this is NOT a good idea, but then neither is deliberately setting the system clock incorrectly. (For a start, if the clock is miss-set, then you will have difficulty syncing it with an external time source like an NTP server. That means that your system's clock will drift.)
The problem i am working on is the TOTP algorithm. The thing I was wondering is how would it work if the time on server and the one on the device are not the same.
I see. The answer is that TOTP cannot work if the two clocks are not synchronized to within a small multiple of the timestep.
1 - There's another way too. Write an application that can do some image processing on a picture of a clock to read the time. Then hook this up to your computer's video camera, and point the camera at a cuckoo clock hanging on your wall. Make sure you wind up the clock regularly. If you want the date as well, point a second video camera at your Dilbert desk calendar.

Android one time tutorial

I am trying to design an app and am really new to both coding and android development.
In my app, (a homework planner that uses SQLite Database), I want a screen to prop up once, the first time the app is launched, where the user enters a number of classes (such as math, english etc).
Anyone know how I can accomplish this? one of the main problems is how to have a screen that only runs the 1st time and then never again.
Thank you!
You could use SharedPreferences to store an attribute like 'firstTime'.
The first time the application is opened, you could alter the value and check it everytime the app is opened.
You can check your database, if the user entered a number of classes. If he dont, show this dialog, else not.
There are many other ways you could solve your problem!

How to make Bukkit not save chunks automatically at any time

Bukkit saves automatically every few minutes, and also saves when it shuts down.
I need a way to not save the chunks, because I'm in need of a system to make my minigame map to be completely fresh when the server restarts, for another round of the minimap.
What I tried to do:
Setting the save-automatically off in the server.properties;
Running the following to try to shutdown without saving:
Bukkit.getServer().unloadWorld(getServer().getWorlds().get(0), false);
Bukkit.shutdown();
What you have tried is the correct way to do this.
The problem is that you cannot unload default worlds (which are the Overworld, the Nether and the End).
You have to create a new one.
You need a WorldCreator object.
WorldCreator seed = new WorldCreator("arena");
World arena = seed.createWorld();
This is a custom world: regions are saved (if not set otherwise) but Bukkit will not know about it after the server restarts.
So you must run the code above the first time to generate the files, then to load them from disk if found.

Categories