how to use session in android - android

I read shared preference is used as a session for small data storage. I would like to know , what are the way to manage session in android and which is more efficient and useful way to use session. please guide me.

It can be as simple as storing a datetime string lastUsedDate in SharedPreferences, and looking up lastUsedDate everytime the app's onCreate() is called and calculate the time passed since.

you can manage your session via api, you can store a value in shared preference when your app is lunched. This start a session when you store a value , when you need to close this session or log out just change your share preference value and update your api and your api return a value which define your app state

you can use your sqlite to store sessions..you can also make use of Bundle.
Before you can talk about the one most efficient in storing session, the data size will have to be considered.
Your data storage options are the following:
Shared Preferences
This stores private primitive data in key-value pairs.
Internal Storage
This stores private data on the device memory.
External Storage
This stores public data on the shared external storage.
SQLite Databases
This stores structured data in a private database.
Network Connection
This stores data on the web with your own network server.
you can also check here for more explanation

Related

Android shopping cart : How to keep products when user exit from app?

I'm developing an e-commerce application like flipkart or amazon , i need to keep the products in the cart even if user accidentally exits from app and iam following tutorial
http://www.androiddom.com/2011/06/android-shopping-cart-tutorial-part-2.html
can anybody tell me how to store products even after exiting from the app ?
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following, but for your specific case i would go for Shared Preferences:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
To store data you would typically use a Database. Personally I would recommend Realm as a local database for Android given it's simplicity and good documentation
You can use shared preference to store local cart data.
Also, SQLITE db is another option to store users cart data.
You can use a SQLite database.
Here's an official documentation.
You can use a database (SQLite) or use a sharedpreferences key with a string of the selected products
You can use sharedpreferences like so:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putString("cartporducts","porduct1;porduct2;porduct3;porduct4;...").apply();

Android - Security On Saving Data

I wanna save a user's score that is retrieved from a server, locally, in order to reduce number of calls to the server.
However if I use SharedPreferences, it could be easily edited by the user. If I use a local database, it could also be changed if user has root access.
So is there any way to safely store data? Should I encode it?
Note that I can't check if local score equals server score, because it may be modified by the app.
A better way to enforce security while saving data in either to save it on your server and enforce security there. But, if you want it to be saved on local; then encrypt them and save them.
For sqlite encryption; https://guardianproject.info/code/sqlcipher/
is great. (link to project)
For saving files or blob data https://github.com/facebook/conceal may
help.
For saving simple key-value pairs in sharedprefs -
https://github.com/scottyab/AESCrypt-Android is useful
It is not good to save sensitive data such as user's personal information and/or passwords in shared preferences or raw sqlite database without encryption.

How to keep shared preference data after clearing the cache of application from settings

I have observed that when i click on clear cache in settings>apps>particular app,
Shared preference data get deleted.
how to keep shared preference data even if i clear the clear the cache? is it possible? if possible means give idea about that
It is worth noting that there are three types of data clearing in Android, of which your application has no control over:
Clear Data
Clear Cache
Clear Defaults
The only way to have persistent data is to use the SD card, but again, users won't like to have the data on their card after the app is uninstalled or users can un-mount the SD card.
Or you can consider:
Storing the data on a remote server with some kind of authentication to retrieve it
Using Data Backup service
Shared preferences is generally used to store temporary information on a user's device. So generally it holds temporary information/data.
To store data that will survive the 'Clear Cache' action, you can store information in an sqllite database.
To do this, you need to implement a content provider that will encapsulate access to the sqllite database, it will help you store and retrieve data that will not be deleted when the cache is cleared. find more information on how to create a content provider here : http://developer.android.com/training/basics/data-storage/databases.html

Difference between shared preference and sqlite

I know this topic has been discussed before on Stack Overflow. But there are still some things that are not clear when I read previous posts about it. So here they are:
I know that we use shared preference for small datasets and sqlite for large data manipulation, so if we just want to save a username and password should we use shared preferences?
Won't shared preferences be lost when user uninstalls the app? For example I download an app called abc and save my username and password. Then I uninstall this app from one phone and try to access it from other phone using the same username and password. Will this be saved using shared preferences or the data be lost?
What are the main reason we use one over the other beside large and small datasets?
You can think of the difference between shared preferences and an SQLite database in terms of data size but that isn't entirely accurate. A better way to think of it is in terms of the structure of the data you want to store.
Shared preferences can only store key-value pairings whilst an SQLite database is much more flexible. So shared preferences are particularly useful for storing user preferences, e.g. should the app display notifications etc. Whilst an SQLite database is useful for just about anything.
Both data sources are local but something you should be aware of is the ability to backup your application data to cloud storage that is linked to the user's Google account. This makes it much easier for your users to change devices and for their applications to easily transfer to the new device. For more info take a look here.
In the situation you described about you will lose the user name and password in both situations. The data is stored on the phone, when you uninstall the application, the data that some with it will also be lost. The user will have to re-enter this information.
You can save the user name and pass in either the shared Preferences or a DB, that is personal preference. Just make sure you lock either down, i.e. don't share the DB or Shared Preferences that you keep this information in.
As for the difference... shared Preferences should hold well... shared Preferences... here is an example:
If I create an option to change the background color, I will store all available options in a DB that can be loaded into a adapter view for the user to choose from. But I will store the color that they have selected in the Shared Preferences. This way when the application load I can get the Shared Preference value of the background color that should be used.
SharedPreferences is used for just that, storing user preferences shared application-wide. You can use it, for example, to store a user's username, or perhaps some options he or she has configured in your app in which you want to remember.
SQLite is a relational database. It's used to store your application's data, not preferences or configuration information.
Both are stored locally on the device.
1.SharedPreferences stores only Boolean, int, float, long, String five kinds of simple data types, such as can not be conditional query. So, whether SharedPreferences data storage operation is how simple it can only be a supplement of storage, but can not completely replace other data such as the SQLite database is stored.
2.SharedPreferences based on the XML file to store key-value key used to store configuration information(mainly user preference for your application).
3.Sharedprefrece just like cookies in web which store some basic information at client side.
both store their data locally, so uninstalling the app will delete both. other than that, SharedPreferences is easier to program, and you're right about the data amounts.
In general, shared preferences should be used if you want to allow your user to directly manipulate certain data fields. Shared preferences are basically user preferences; if you would like the user to reconfigure the app to behave in different ways, you should expose that functionality as a shared preference. On the other hand, the SQLite database should be used if you want to limit the visibility of the data to just the application, if you want a stronger guarantee that the data be persistent, and if you want the application to behave independently of what is stored in the database. Of course, you can use both in one application.
Shared preferences and the database are part of local data that the application stores. If you uninstall the application, both of the data stores will be removed.

How to store data from my app

Actually i want to know how to store data from my app in the device so that i can review the store data when i run the application again..
means in simple terms i want to say that suppose i have text box where i write some information..now when i click the submit button, this information will be save,so that when i open the application the stored data should be appear in the text box..
In all terms i want to say that i just want to stored data in the way that we are using database for storing data..so please anyone suggest me how that can be done in android.
if possible show with an example
Regards
Anshuman
If you have to store small amount of data, you can use SharedPreferences in Android.
If the data that you have to store is big/complex enough, try using SQLite database.
Still need help?
UPDATE: There's a tutorial that I wrote to demonstrate how to use SQLite database. check it out here. Although it copies existing database into device's memory, but other versions of it, which create database through code can also be devised from it.
A better tutorial is here : http://www.vogella.com/tutorials/AndroidSQLite/article.html
1) If you want to store data in table format then you can use SQLite database in android
2) If you don't want to store data in table format then you can store in SharedPreference
more info about SharedPreference here and here
Android comes with a built in SQLite database that you can use. I advice you to go trough this notepad tutorial. It teaches the basics of using Android SDK including different states of the android application as well as how to use SQLite with Android.
For storing simple key = value pairs, you can use Properties.
For data storage as in a database, you can use sqlite on android.
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
Data Storage

Categories

Resources