when should I save quest progress to server? - java

I am making a game for Android/iOS. I have recently got daily quest to work nicely, the only problem is I can't figure out when I should save quest progress.
I don't want to make a servercall everytime you do something in the game that could effect quest progress.
Say for example your quest is "win 3 games", I was thinking of just putting quest progress in preferences until you completed the quest, but that can lead to a bunch of unwanted behaviour if you exit the game when your progress is for example 2/3.
Is there a smart way to go about this or do I just have to make a servercall and save directly into db every time you do something that can effect a daily quest?
As far as I know there is no way to do it when the user exits the app?

Is there a smart way to go about this or do I just have to make a
servercall and save directly into db every time you do something that
can effect a daily quest?
iQue, make a server call every time and validate the data they are sending you (did they really achieve what they're claiming?), users can root the device or modify the game client, so this is the only correct way to do it.
As far as I know there is no way to do it when the user exits the app?
If you want to do anything when the app is exited on Libgdx, there is again only one correct (in the spirit of Libgdx) way to do it and it's not mentioned in the other answers, as you can write this cross-platform without delving into Android/iOS specifics: override the dispose method of your main game class (ApplicationListener).

Is there a smart way to go about this or do I just have to make a
servercall and save directly into db every time you do something that
can effect a daily quest?
I'd choose server call.
As far as I know there is no way to do it when the user exits the app?
There are onPause, onDestroy methods in android. You can use SharedPreferences and when user exit the app, delete that data.
But whatever you do, never trust the client data. Always try to validate data in server.

I would save it as soon as something changes. Your server will probably be able to handle this and you don't need to mess around with platform specific stuff.
If you really want to avoid this for some reason you would have to look at the platform specific ways to execute code just before the app exits. On iOS this seems to be applicationDidEnterBackground() on Android you can use onDestroy() .

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

Is SharedPreferences suitable, or is there a better alternative?

I am trying to become familiar with android application development. I am trying to make a simple app to learn the basic skills, but I am not sure how to handle this part of the app. Therefore I need advice about how to go further.
I have a simple app with a button and a textview with 0 on it: the score. When the button is clicked, score goes up by 1 (by simple java code). When I close the app and restart, the score is 0 again.
What I want: When closing the app, the score must be saved, so that the same score appears on restart. For example, like that egg that you have to click on a million times. If several users had the app, everyone should have their own score (of course). The number should therefore really be linked to phone/account.
I already read about sharedpreferences. It sounds like what I'm looking for: Storing variables. However, I don't know if this is suitable for what I want to use it for. The score should never be lost for that specific phone/account. It may not be possible for the user to adjust the score himself (except by clicking on the button).
My question: Is sharedpreferences suitable for what I want to use it for, or is there a better alternative?
SharedPreferences is used to store small pieces of data locally on your device. So, if the user Uninstall the app or Clears App data, the the data you store in SharedPreferences will be cleared.
If you are okay about data being lost on Uninstall/ Clear Data, SharedPreferences is surely the best way to store such small piece of information as using an Online database is quite an overkill for such small piece of data.
The score should never be lost for that specific phone/account.
But according to this, it seems like don't want to lose the data even if the user Uninstall/Clear Data, clearly you have to use a Cloud Database like Firebase Real-time database/ Firestore.
If you want users to Login/Sign Up, you have to use Firebase Auth otherwise, you can simply store data as PhoneNumber : Score key-value pairs on Firestore.
Hope it helps

How to store and read user usage data of my application in Android using java?

I have created a an application for a project - it's very basic... what I would like to do next is see how users are using my application e.g. buttons pressed, which page is viewed the most, for how long etc.
I am new to java and I do not understand how I can implement such thing; I have an idea but do not know whether it is efficient;
I would add a counter for each of the buttons in my app, whenever a button is pressed the counter increases by 1 and so on and so forth;
To see how long a user stays on a page, I could add a timer when the user enters the page, timer starts and stops when user exits.
...
Would something like this is viable and efficient? are there better ways of implementing such algorithm.
Not sure if there are, I searched but couldn't find any, does google offer such service like they do for websites with google analytic.
I am sorry, I've no to show this, as I haven't actually starting doing it. Wanted to get a grasp of it before I do and find out whether it is the correct strategy.
I would really appreciate your help.
https://developers.google.com/analytics/devguides/collection/android/v4/
Analytics for android apps maybe its what u are looking for
Start here: http://www.google.com/analytics/mobile/
https://developers.google.com/analytics/devguides/collection/android/v4/
You could also go with that you stated already, and add those values to an array. Just note that this will require you to turn on some permissions which might make your app unpalatable for some individuals.

How/Where to implement onItemClick handlers with several fragments

I am currently a student taking a Mobile Device Applications development class and am getting stuck on my final project, an Address Book style app. I have used the example from our book (Dietel's Android for Programmers An App-Driver Approach, Volume 1, Second Edition) as the basis and have been editing code from there. The biggest thing I want to change/enable is to allow the user to select different fields within the contact (address, email, etc) and pass an Intent that will open Maps when address is selected, eMail when email is selected and so on. Where I am lost is where to place my listener and declare it. Our book and subsequent searches are not really clearing this up at all. I understand the onItemClick call, but am not sure where to place it, within MainActivity.java or the DetailsFragment.java, which is all the code to handle anything that happens when the contact is open and displayed. I feel like I can put it all within the DetailsFragment.java but have a nagging suspicion that I am missing something. I have not had the time to edit and test this yet as I am actually heading out for work in a minute but I thought I could maybe get a better understanding or maybe even better links to read to learn from as my Google search doesnt seem to address what I am looking for. Also, not sure if it matters or not, but I am not using a ListView at all and everything I find tries to force a ListView use. Thank you guys for your help/consideration and time.

Since when is the phone charging/discharging

I wanted to learn more about the Android Services / Broadcasts, so I started a simple project, to create a battery monitoring app. It turned out pretty good, I'm using it for a few days now, but I want to add a new function: to show since when is the phone charging/discharging.
First I thought that I would create two static fields in my BoradcastReciever extension class, where I get and publish the data about the battery, one for the actual state (charging/discharging), and one for the time, when the change in state happened. This way, I could just subtract from the current time the last change, and know exactly since when is the phone charging/discharging.
But there is a problem with this solution: It won't show the correct data at first, when a user starts the app. I wouldn't make a big deal of it, but I saw that Android tracks this data somewhere, because inside my phone settings I found this information, so why take the hard way.
So my question is: is there an easy way to get from the Android system the date/time (no matter what format) of the last charging state change?
I looked at the BatteryManager reference but there are no constants named after what I seek, and which I could use, to get the information from the Intent of my receiver.
The Android OS tracks the connect/disconnect of a power source, but does not make this data accessible to apps. You have to record this all yourself, using intent filters.
The two intent filters to use are android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED; with these, you can monitor when the power source is connected and disconnected.
You can find information about this process explained incredibly clearly here. Another blog describing the process can be found here.

Categories

Resources