Android store data
Sorry this page not ready...
Different Ways to Store Data in Android
Android offers several options for storing data, each with its own strengths and weaknesses. Here's a breakdown: Shared Preferences What it is: Shared Preferences is a simple key-value storage system for storing small amounts of data. How it works: You store data as key-value pairs, where the key is a string and the value can be a primitive type (e.g., int, boolean, String). Pros: Easy to use. Good for storing small amounts of data. Persistent across app sessions. Cons: Not suitable for large or complex data. Limited data types. Not secure for sensitive data. Use Cases: Storing user settings. Storing small amounts of data. Storing simple data types. Example: 1: import android.content.Context
2: import android.content.SharedPreferences
3:
4: fun saveUserName(context: Context, userName: String) {
5: val sharedPreferences: SharedPreferences = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
6: val editor = sharedPreferences.edit()
7: editor.putString("userName", userName)
8: editor.apply()
9: }
10:
11: fun getUserName(context: Context): String {
12: val sharedPreferences: SharedPreferences = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
13: return sharedPreferences.getString("userName", "") ? : ""
14: }
Internal Storage
What it is: Internal storage is the storage space that is allocated to your app. It's private to your app and cannot be accessed by other apps. How it works: You store data in files within your app's private directory. Pros: Secure. Private to your app. Fast access. Cons: Limited storage space. Data is lost when the app is uninstalled. Use Cases: Storing app data. Storing sensitive data. Storing data that should not be shared with other apps. Example: 1: import java.io.File
2: import android.content.Context
3:
4: fun saveFile(context: Context, fileName: String, data: String) {
5: val file = File(context.filesDir, fileName)
6: file.writeText(data)
7: }
External Storage
What it is: External storage is storage space that is shared between apps. It's typically located on the SD card or in a shared directory. How it works: You store data in files on the SD card or in a shared directory. Pros: Large storage space. Shared between apps. Data is not lost when the app is uninstalled. Cons: Less secure. Requires permission to access. Can be slow. Use Cases: Storing large amounts of data. Storing data that should be shared with other apps. Storing data that should not be lost when the app is uninstalled. Example: 1: import android.content.Context
2: import android.os.Environment
3:
4: fun saveFile(context: Context, fileName: String, data: String) {
5: val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), fileName)
6: file.writeText(data)
7: }
SQLite Database
What it is: SQLite is a lightweight relational database that is used to store data in Android. How it works: You create a database using SQL commands, and then you can use SQL commands to access and manage data. Pros: Powerful. Flexible. Good for storing structured data. Cons: Requires a lot of boilerplate code. Difficult to manage complex data. Not secure for sensitive data. Use Cases: Storing structured data. Storing large amounts of data. Storing data that should be shared between apps. Example: 1: import android.content.Context
2: import android.database.sqlite.SQLiteDatabase
3: import android.database.sqlite.SQLiteOpenHelper
4:
5: class MyDatabaseHelper(context: Context): SQLiteOpenHelper(context, "MyDatabase", null, 1) {
6: override fun onCreate(db: SQLiteDatabase) {
7: // Create the database
8: }
9:
10: override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
11: // Upgrade the database
12: }
13: }
Room Persistence Library
What it is: Room is an abstraction layer over SQLite that makes it easier to work with databases. How it works: You define entities, DAOs (Data Access Objects), and relationships using annotations. Room then generates the code to create and manage the database. Pros: Easy to use. Type-safe. Handles complex data. Handles errors and exceptions. Automatic parsing of responses. Cons: Requires external dependencies. Learning curve. Requires a bit of setup. Use Cases: Storing structured data. Storing large amounts of data. Storing data that should be shared between apps. 1: import androidx.room.Database
2: import androidx.room.Room
3: import androidx.room.RoomDatabase
4: import androidx.room.Entity
5: import androidx.room.PrimaryKey
6: import androidx.room.Dao
7: import androidx.room.Query
8:
9: @Entity(tableName = "users")
10: data class User(
11: @PrimaryKey(autoGenerate = true) val id: Int,
12: val name: String,
13: val age: Int
14: )
15:
16: @Dao
17: interface UserDao {
18: @Query("SELECT * FROM users")
19: fun getAllUsers(): List < User >
20: }
21:
22: @Database(entities = [User::class])
23: class MyDatabase: RoomDatabase() {
24: // Add the database
25: }
26:
27: fun main() {
28: val db = Room.databaseBuilder(context, MyDatabase::class.java, "MyDatabase").build()
29: val userDao = db.getDao(User::class.java)
30: val users = userDao.getAllUsers()
31: // Handle the response
32: }
Internal vs. External Storage
Internal Storage: Private: Internal storage is private to your app. Security: Internal storage is more secure than external storage. Fast: Internal storage is faster than external storage. External Storage: Shared: External storage is shared between apps. Security: External storage is less secure than internal storage. Slow: External storage is slower than internal storage.AndroidJavaLearning context:
Comments (
)

Link to this page:
http://www.vb-net.com/AndroidConcept/Index06.htm
|