Displaying a message one time only - java

I want to find a way to display a notification (like a JOptionpane, a JLabel or any other type) only one time after a user launches my application that is formed in a.jar file.
By only one time, I mean that the user gets a one-time notification after the first use, then for every following times my application runs, this notification should not appear.
My application uses Java Swing. Is there a hint how to make a message pops up from the main JFrame for example?

You simply need to know whether this application has already been running in that environment before or not. A simple way to do that is to:
Check whether some file with a particular name exists in the working directory
if it doesn't: show your notification, then create the file
if it does: don't show your notification
Sample Java code:
private static void notify() {
final File file = new File(".launched");
if(!file.exists()) {
// show your notification HERE
file.createNewFile();
}
}

Check for stored value on disk indicating the message has been shown
If not, show the message and store the value on disk.

You can do that by setting up one preference. I think is the most straight forward way to do it. Use the preferences class.
The preferences are loaded at starting, then you ask if your "boolean_first_use" is false or true. After that you set it to false, as you know that the user is having that first time message. So next time, it will not fire the notification.
http://www.vogella.com/tutorials/JavaPreferences/article.html
http://www.javaranch.com/journal/2002/10/preferences.html

Related

So am building an app and it has this feature that i dont know exactly how to go about it

I hope this makes a bit sense, basically, I have this feature in my app for tracking calories which consists of having this page that only appears the first time you use the feature and it asks you to add personal details (so it can make the right calculations), after that you get faced with a simple page that tracks your nutrition with a button for the user to insert the meals he has eaten, this page has to save the inserted data (via firebase) and then restart from 0 each and every day.
my first problem is I don't know how I make the page that only appears one time to save personal data(to be more precise I don't know how to make only appears the first time). and the second problem is how do I make the app automatically sends the given data at the end of each day?
interface in normal state, interface when adding the meals
hopefully, this 2 images will help you get a better grasp of what am trying to explain
don't worry am not looking for someone to straight up solve it all for me, I just need some orientation about what type of things/functions I need to do to solve these 2 problems
While #Narendra_Nath's answer might work, please note that is not a bulletproof solution. Why? Because a SharedPreferences doesn't persist across app uninstalls. This means that your user can install and uninstall the app and see the page as much as they want. So if you indeed want a user to see a screen only once, then you should consider storing that data in a database. Please note that SQLite isn't also a solution because when a user uninstalls the app, everything that is stored locally is wiped out. So what's the solution?
The best way to solve this would be to store the data in the cloud, either in Cloud Firestore or in the Realtime Database. So you can set a boolean variable and always check against it.
If you however intend to implement Firebase Authentication, then another solution would be to display the screen when your users are authenticated for the first time. So even if they will try to sign in on another device, install and uninstall the app, they won't be able to see the screen again.
Regarding the second problem, you should consider using Cloud Function for Firebase. It's the most elegant solution. If you want to somehow schedule an operation, then you should consider using Cloud Scheduler, as explained in my answer in the following post:
Is it not possible to have a code in the background who will be called every 24h?
Make the page that only appears one-time -> store a value in the shared preferences "isInfoShownToUser -> false" then do a check when the app starts to check if this value is false or true. If it is false show the "take the info" page .. then turn the value to false in the shared preferences.
How do I make the app automatically send data -> Use a Workmanager implementation to send data to the server (Firebase) at a particular time ..
Or use a implementation like the first one which uploads the data to the server just once everyday

Java (Android) File object too slow for AsyncTask

In an Android application I'm developing, I need to create a backup of a file and check on start up if it is identical to a remote file, if they are have different bytes then overwrite the backup with the remote file, afterwards check if they are the same, if they are identical return true. To do this I have the following process:
if(!backupFileExists(){
backupFile.createNewFile();
}
if(!checkBackupAndRemoteFilesAreIdentical()){ <----First Time
if(overwriteBackupFileWithRemoteFile()){
if(checkBackupAndRemoteFilesAreIdentical()){ <---- Second Time
return true;
}
}
}
return false;
The problem is when i run the code with AsyncTask, the second time i run checkBackupAndRemoteFilesAreIdentical() the value of the backupFile.length() hasn't updated, so it returns 0 which then returns false.
However if I add Thread.sleep(5000) the value of the backupFile.length() has time to update, it is successful and returns true.
Is there anyway to have this work without the Thread.sleep(5000) ?
This is normal, a network operation will take longer than loading your first activity.
If your 1st activity requires this file (or a portion of it) in order to work, then, you need to get it from the server (yes! in an async task) and update an object that your application will rely on. Decouple your application logic/design from source of data.
This problem is a generic one, what we do in general, we get as little data from the server for the first activity to work, then, when the user navigate to other screens, the data will be available at this stage.
I would suggest to think about your application data design. Which Server Data you need in every step and how this will be updated. If you are the owner of the server, and you are able to create new endpoints, try to design fast endpoints, less data, expose 'http head' operations to get only the content-length, ...
If you have more details on what you're trying to build, I will be more than happy to go through specifics.

JavaFx Do something on first load [duplicate]

This question already has answers here:
Detect first time user in java app
(3 answers)
Closed 6 years ago.
I would like to pop up a window to select a file location when the user launch the software for the first time. I'm new to javafx and I looked for an answer on the web but no success...
Thanks in advance
I think I found it.
primaryStage.setOnShowing(event -> {
//Code here
});
It does action on first startup. I don't know if it's the best way to do it but that's how I did it. I already have a file with about 3 lines. I just added a 4th one with a random word and when the user launch the app it check if the word exist in the file. If so, it does nothing. If not, it ask the user to select the folder and if the selection is successful, it write the word.
Firstly, I would want to point out that your phrasing probably isn't clear enough for most people to understand exactly what you need. I am going to assume you have some kind of settings (like default application storage directory) which you need the user to specify at the first time the JAR is run. If the JAR file is run subsequently, it should not prompt for that again and use the settings previously specified.
Typically, when the user runs the JAR file, all the data would be isolated within that session. If the user closes the application and opens the application again, it would behave just like the previous run.
If you need to persist these data or settings, you can use Properties. This will save data in a separate file. The normal convention is to save it in the same folder as the JAR file, and named as config.properties.
At the start of the application, you should check if this file exists, if it does not exist, it means that this is the first run. Subsequently, when the user set the data (e.g. file folder), you would save it to the file.
You can find an example here.
Background
In order to implement a file selector, we can make use of JavaFX’s FileChooser. This will open a window giving us the opportunity to select a file.
What you’re asking for is for a FileChooser to open prior to entering the actual application. Let’s have a look at the implementation for something like that!
Implementation
At first, we’re going to need a JavaFX Application class that will open a window if we were to create a new instance out of it:
public class App extends Application
{
private final File file;
public App(File file)
{
this.file = file;
// Optionally provide ‘launch’ with some arguments
Application.launch();
}
#Override
public void start(final Stage stage)
{
// ...
stage.setScene(new Scene(insertNodeHere));
}
}
As I’m certain you’re already aware of—a class like this will open up a new window. This is the separate application class we’ll be calling once we’ve retrieved a File using the FileChooser in our main class.
In our main class, we’ll put this:
File file = fileChooser.showOpenDialog(stage);
if (file != null)
new App(file);
This will launch your application if the selected file didn’t turn out as null.
Moreover
With the implementation above in mind, you can complicate things as much as you feel like. Perhaps you’d like the application to start even if the file were null? In that case, there’s no need for the if statement.

Gwt localStorageEvent - cross tab communication

I want to make cross tab communication. On that i would like to use localStorage / sessionStorage.
Class1 contains this part of code:
Storage stockStore = Storage.getLocalStorageIfSupported();
if (stockStore != null) {
stockStore.setItem("newLoad", "123");
}
Class2 contains this part of code:
Storage stockStore = Storage.getLocalStorageIfSupported();
if (stockStore != null) {
stockStore.addStorageEventHandler(new StorageEvent.Handler() {
public void onStorageChange(StorageEvent event) {
Log.println("Heureka!");
}
});
}
Code in class one is called when user push button.
So when user have open two tabs and he push button (inside tab 1) which will invoke code in class1. Then event is fired and "Herueka" is written but only in his actual tab (tab 1).
It should be written in both tabs (tab 1 and tab 2). So this is not working.
url of tab 1: http://127.0.0.1:8888/#loads
url of tab 2: http://127.0.0.1:8888/index.html#lights
Tabs have same session and i am using FF 47 for testing.
Everything is compiled trough eclipse local jetty server and super dev mode is used.
One workaround has been found since the previous answer: https://github.com/gwtproject/gwt/issues/9205 (I have not tried it)
That said, I have confirmed following to work:
private static native void addTrueStorageHandler(Handler h) /*-{
$wnd.addEventListener('storage', function(e) {
h.#com.google.gwt.storage.client.StorageEvent.Handler::onStorageChange(Lco m/google/gwt/storage/client/StorageEvent;)(e);
});
}-*/;
The above function should be called to register the Handler. This just creates a javascript anonymous function to call the Handler. It completely bypasses the entire Storage GWT mechanism.
I can confirm that and I had the same issues trying to utilize the events on the local storage.
Unfortunately the storage events do not seem to work across tabs.
At least I was unable to have them reliably fire for all tabs and I also needed to exclude the current tab from the event (this tab fired it and does not need to be notified).
What I did as a workaround is to create a class, which handles signaling using the local storage.
It is quite a lot of code, which is tied to an internal project, but I will try to outline how it is done to help you getting started.
I don't rely on events here, but use a Timer with scheduleRepeating to periodically check the local storage every 3 seconds.
Every tab needs a different ID, which you can generate e.g. using Random.
If I want to signal something I set an entry in the local storage.
The key of this entry is the signal name, the value is the id of the tab setting the signal.
So when the Timer is executed, I check for an entry with the signal name. If I find one, I check the value to be different than the id of the current tab as I don't want to notify the signaling tab for its own event.
You just need to keep in mind to remove the entry from the local storage on shutdown of your application or after a reasonable amount of time. Otherwise you might encounter the old entry on a fresh start of your application and execute the signal handler again.
After all it is not perfect as there is always the Timer running every 3 seconds and the signaled tab can also take up to 3 s to pick up the signal. However it was the only way for me to get a reliable signaling between different tabs.

How to set timer, even if App is closed?

I habe a app for event informations an app which sends pushmessages. Now I want to give the user the option to disable the messages for a specific time (one hour, one day ...). I know how to dissable it compleatly, but how I can set it like I want (one hour...) even if the app is closed?
Well, the simple solution would be to check if there is a time restriction is present in the same place where you notify user for event information. If there is a time restriction dont show the notification else show it

Categories

Resources