How to implement a persistent queue in Android - java

I would like to implement a persistent queue in Android. Essentially a queue where data is stored until another thread takes it and sends it somewhere. The order in which data is stored or consumed is not important (ex. FIFO, LIFO, LILO, etc). Is there a modern library in Android that supports this?
I found a previous question on SOF but this dates from 2012: How to realize a persistent queue on Android

There are a lot of way to do this. You can persist the collection by saving it into a database like SQLite, serialize it as a file and deserialize it when you need it, or you can save it in SharedPreferences of Android.
Anyway there is a cool library called Hawk
(https://github.com/orhanobut/hawk)
that is a
Secure, simple key-value storage for android
How Hawk works:
If you take a look at the Hawk source code you can see that it uses SharedPreferences to save data to the disk. It serializes the object you want to persist and saves it into SharedPreferences as a String.
Quotes and images are taken from Hawk github page.
You can find other alternatives here (in the "Persistence" section) : https://github.com/codepath/android_guides/wiki/Must-Have-Libraries

You can check out Priority Jobqueue
It has an option to persist the jobs and prioritise them.

Related

How can I save the data my app parses, so that it doesn't need to be parsed when the app is reopened later?

So I am working on my app, and it parses data from the LoL (League of Legends) API, so far so good, but the only problem is that if the user closes the app, and reopens it parses the data again. I want the objects I parsed from the JSON data to be saved, once it initially parses them, instead of redownloading them every time I reopen the app. It slows my app down.
There are a few ways to do that, but they all basically doing the same thing: serialize/de-serialize objects. The difference will be mostly what kind of persistence storage and what serialization mechanism are you using.
As for persistence you can use for example file, data base, etc.
Java provides human-unreadable binary serialization. Instead you can use JSON, XML, etc. formats. I assume Java built-in serialization will be the fastest, but it won't be deserialize if your class changed in the mean time.
Local persistence also would allow to map objects you parsed into database and load them pieces by pieces as needed.
Save your JSON data as a string in Android SharedPreferences. It is the most convenient and fastest method for you.
Using SharedPreferences

Android: Performance when calling Json file data multiple times from different classes

I'm working for my first time with Json files as data for my app, and I have been thinking about how is the best option to save a json file to make it accessible from different classes.
It is easy, I have a JSON file with different rated for currency conversions. I need to convert the data that I show in different currencies in different modules of my app continually.
So I just want to know a good way to store my json data and not load my Json file every time that I need to check a currency rate and find a way to do it more simple, maybe as a hashmap in my BaseActivity which is extended by the rest of my activities and just call it, or a static class with all the methods related with currency rates. I also thought about save it in a sharedPreferences.
It just a question for check the opinion of more people about the best way looking for app performance to continually call data from Json files.
I am not a financial expert but as far as I know currency conversion rates are applied on a daily basis. Also I have not worked on Android but the answer below is more problem-specific rather than technology specific and hence I assume that you would be able to map this to Android primitives.
I would hence design my strategy for maintaining the currency conversion rates as below:
Fetch the currency conversion rates the first time the app starts. Process this data and store it in memory within a HashMap for the lifetime of the app. Also, place this data into a more permanent store (e.g. SharedPreferences).
Refresh this data periodically using a service that runs once every
24 hours fetch the updated data. Process the fetched data in a
manner similar to above.
Notify the app that the currency rates have been refreshed. In case
the app is running it can referesh it's memory caches.
As about the class design - the hash map storing the currency exchange rates should be stored in a separate class say CurrencyConversionRateCache. This class exposes all method required for your activity classes to fetch conversion rates. The class also gets notified from the background service (possibly using Intents in Android) when new currency conversion rates have been downloaded. This way the cache itself is responsible for ensuring the validity of it's data keeping activity classes simple.
Hope this gives you some headstart towards solving your problem.

Store and retrieve arrayList from internal memory?

I'm building a home replacement app. I need to store the ArrayList with the apps the user picked to show on the launcher in the internal memory. I mean the array mustn't be deleted when the app is closed.
I'm very close to finishing the app and I don't think I'll work a lot more on Java, I'm not a programmer so I just want the easiest way to do it. How can I store and retrieve an ArrayList in the internal memory?
Use SharedPreferenes to store and retrieve the arrayList..
Check this link..
Save ArrayList to SharedPreferences
You should store such data as Arraylists in the database and when you relaunch the app just fetch the data from database(SQLite) and display it to the user.
Heres a good tutorial on android SQLite.
Take a look at this link, for all the different data storage mechanisms in android. But for your requirement I would suggest using a db.
The easiest way is to use Java serialsation. It writes the content of an object to a (file-) outputstream. Thats a few lines of code.
a bit better (faster, less bytes, readable by other non java systems) is to uses a custom serialisation, using DataOuputStream. That creates a binary file in the format you define.

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