Skip to main content

Create a simple Android app that uses Encrypted Preferences with a Singleton architecture

Creating a simple Android app that uses Encrypted Preferences with a Singleton architecture involves several steps. Below is a guide to help you set up this app. We'll use Kotlin for this example.


Step 1: Set Up Your Android Project

  1. Open Android Studio and create a new project.
  2. Select an Empty Activity template.
  3. Name your project and choose Kotlin as the programming language.

Step 2: Add Dependencies

In your build.gradle (app level), add the dependencies for Encrypted Preferences:


dependencies {

    implementation 'androidx.security:security-crypto:1.1.0-alpha03'

    // Other dependencies

}

Step 3: Create the EncryptedPreferencesManager Singleton

Create a Kotlin file EncryptedPreferencesManager.kt in a suitable package (e.g., com.example.myapp).

package com.example.myapp

import android.content.Context

import androidx.security.crypto.EncryptedSharedPreferences

import androidx.security.crypto.MasterKeys


class EncryptedPreferencesManager private constructor(context: Context) {


    private val sharedPreferences = EncryptedSharedPreferences.create(

        "my_encrypted_prefs",

        MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC),

        context,

        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,

        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

    )


    companion object {

        @Volatile private var INSTANCE: EncryptedPreferencesManager? = null


        fun getInstance(context: Context): EncryptedPreferencesManager {

            return INSTANCE ?: synchronized(this) {

                INSTANCE ?: EncryptedPreferencesManager(context).also { INSTANCE = it }

            }

        }

    }


    fun putString(key: String, value: String) {

        sharedPreferences.edit().putString(key, value).apply()

    }


    fun getString(key: String, defaultValue: String? = null): String? {

        return sharedPreferences.getString(key, defaultValue)

    }


    // Add other methods as needed

}


Step 4: Use EncryptedPreferencesManager in Your Activity

In your MainActivity.kt, you can use the EncryptedPreferencesManager to save and retrieve encrypted preferences.


package com.example.myapp


import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        val encryptedPreferences = EncryptedPreferencesManager.getInstance(this)


        // Save a value

        encryptedPreferences.putString("example_key", "example_value")


        // Retrieve a value

        val value = encryptedPreferences.getString("example_key")

        println("Retrieved value: $value")

    }

}




Popular posts from this blog

How to setup wifi ADB debugger for android studio

How to setup wifi ADB debugger for android studio  Firstly set your adb path Which you can find here Step 1:  Goto your  Android sdk folder -> platform tools  and copy the whole path For example:  C:\Program Files (x86)\Android\android-sdk\platform-tools Step 2:  Goto command prompt or Android studio terminal cd C:\Program Files (x86)\Android\android-sdk\platform-tools and presss enter Step 3:  Connect your device & system with same wifi. Step 4:  Type  adb tcpip 5555  and press  Enter . Step 5:  Type  adb connect x.x.x.x:5555 , replacing the  x.x.x.x  with your phone IP address. find out phone IP address Settings -> About phone -> Status  (some phones may be vary) Note: Incase if you connect more than one devices disconnect other phones except which one you need to connect

How to create google map with drag marker to get user location with auto correct location edit text ...

For drag and drop with user location You have to create a google map fragment activity below are the xml code of google map xml <? xml version ="1.0" encoding ="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" xmlns: tools ="http://schemas.android.com/tools" android :layout_width ="match_parent" android :layout_height ="match_parent" tools :context =".GetUserLocationActivity" > < fragment android :id ="@+id/mapuserloc" android :name ="com.google.android.gms.maps.SupportMapFragment" android :layout_width ="match_parent" android :layout_height ="522dp" app :layout_constraintEnd_toEndOf ="parent" app :layout_constraintStart_toStartOf ="p...

create MVVM Architecture Pattern for my android app

Here is an example of how you could implement the MVVM (Model-View-ViewModel) architecture pattern in an Android app using Java: Create a View class, for example MainActivity that extends AppCompatActivity and is responsible for handling user interactions and displaying data to the user. public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private MainViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); viewModel = new ViewModelProvider(this).get(MainViewModel.class); binding.setViewModel(viewModel); binding.setLifecycleOwner(this); } } } } Create a ViewModel class, for example MainViewModel that extends AndroidViewModel and is responsible for holding and processing the data for the View , as well as handling any business logicy code public cla...