Android game, options about storing user data - java

I'm developing a little game for android and i'm at the point where i want to save data of the progress the user has made so far.
I read about sharedpreferences, SQLlite... i have used some XML parsers aswell but my question is, if i want this:
Fast performance.
Secure or encryptable.
I want to point out that the data to save is not very long, profile name, levels completed, faction selected...
Thanks in advance!

Any solution will be "fast enough" for loading/saving the data... and any of them will be encryptable (as in they can all store string data).
SharedPreferences would probably be the simplest way to go. Perhaps store your data as JSON, then store the entire JSON string in SharedPreferences. Then you can easily add encryption to it later.

Related

How I can create history layout in android studio such as recent

I am building an app include edit text and button. The user can put text in the edit text, then click button to transfer him to the browser.
So, I would like to create other layout as recent to store the text that the user entered on the edit text.
How I can do that? I need the logic or code that can help me!
Also, should I have create database to store the data?
Example:
enter image description here
You can store data in multiple ways, and you will need to understand what is right for your use case. You can store data in memory, by simply creating a List with your data type and adding to it every time user will click a button, but then it will not persist between sessions with the app.
If you want the data to persist, then you would need to use permanent storage, and there are a lot of options here:
You could use Shared Preferences
You could use File System and save the data to a file
You could use a database i.e SQLite, and store data there
You could use external server, and get the data through REST API.
Generally, there is a good overview of data storage in Android in the documentation which also have code examples.
Every option comes in multiple ways to accomplish it. There are built-in solutions, and multiple libraries to help you with this task, but first of all, you will need to understand what is the predicted usage of this data. I.E Should user have access to the data from another device? Should the data be available offline? Will data have complex structure? How the app can expand in the future? e.t.c.
Only by knowing this you can design how you will handle it.
If you need logic or code to create view, then you will need ListView, or RecyclerView, Adapter for handling the data, extra xml layout file for single item of your data.

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

Android writing class to internal storage

So I'm making a weather app and I found when the app is idle for too long and destroyed that the forecast data is lost. So I think the best solution is to save the data to internal storage and retrieve it when it's needed.
The class containing the data has 2 arrays of other data classes, and it all contains strings, ints and doubles.
What is the best way to save this data? The options I've seen are a preferences file or saving it to a file in local storage.
I didn't think the preferences file would work because it sounds like that is more meant for smaller amounts of data.
I've been trying to save to local storage, but that hasn't been going well. Not too familiar with manually saving bytes to a file, and the Android developer guides aren't very comprehensive at all. I tried to serialize it, but get the not serializeable error. As far as writing the bytes I think I got it correctly written by converting strings to bytes using getBytes, saving the ints as they are, and converting doubles to strings then using getBytes. I'm not sure if that is even the correct way to do it, and I'm not sure how to read the bytes because you have to read a specific number of them. Do I have to find out how many bytes each type of data is going to be? For example finding the type of string encoding and reading the number of bytes for that data type?
What would be the best way of going about this? Should I just use the preferences file? Can someone either tell me how or point me to a resource for reading data from a file like this? Any help would be appreciated.
I tend to use preferences for different amounts of data. Shared preferences save to an xml file. Here is an example:
SharedPreferences.Editor editor = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE).edit();
editor.putString("name", value);
... // add all your other items here to save
editor.commit();
To get the data just do something like this:
SharedPreferences preference = context.getSharedPreferences("pref_name", Context.MODE_PRIVATE);
preference.getString("name", defaultValue);
It is pretty simple and easy to use. I do sometimes save large data sets. For those, I will usually convert the data to json and then save that json as a string. When I get the data I will convert from json to java.
Another option is to use a sqlite database and store all your data there. There are many tutorials and documentation on how to save to a database.
Here is a great read to get you started: https://developer.android.com/guide/topics/data/data-storage.html

Best way to store text data in an android app?

I've made an Android application which contains most used German words and sentences. The application contains a CSV file which stores all the data.
Currently it is working as expected but I want to ask if there is a better way to store such data directly in the app?
I'm also thinking about the ability to update the data via internet like adding new words and sentences.
Thanks!
Miretz
If you want to modify the content (update, remove etc.) I would suggest using SQLite DB which has a pretty nice built-in integration with the Android platform.
There are 2 types SQLDatabaseLite and SharedPreference. Major difference between both is that one is organized and the other not so.
If you need a quick use of a storage facility within your app for example changing text sizes between activity SharedPrefference works best for you.
If you have a complex database system where you need more than one data to be saved for a particular event SQLDatabaseLite is for you example of this is spreadsheet of data for customers; Name, Phone Number, etc.

Android storage advice?

When I log a user in, I pull down some user data. It's just a userid and stuff like that. A couple of strings really. The thing is I don't want to pull this down every time if it's the same user over and over and so I want to store it locally across sessions. I'm aware of SQLite but is making a table just for 1 row really the best solution? Is there not another, better way?
Check the developer.android.com site. Data Storage :
http://developer.android.com/guide/topics/data/data-storage.html#pref
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types
If primitive types is all you need, and the boundary is your application (no multi process stuff), then you should be OK with just using SharedPrefs. Anything else (files,sqlLite) is overkill. SharedPrefs has a clean api that should be sufficient for your needs.
See the developer.android.com site for shared pref usage.
You could use Shared Preferences. It is meant for that.
This other question asks about storing files on the device. The recommendation is to store files on the SD card (if available) and that could be a solution to your problem.

Categories

Resources