EncryptedSharedPreferences

EncryptedSharedPreferences

本文介绍了使用Android的EncryptedSharedPreferences检查值是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在登录时使用EncryptedSharedPreferences来存储值...我想在家庭活动的导航抽屉中更改文本

I use the EncryptedSharedPreferences to store value while login...i want to change text in navigation drawer of home activity

因此,当用户登录时,它将显示注销,否则将显示 login 导航抽屉

so when user is loggedin it will show logout else it will show login navigation drawer

登录活动:--------

Loginactivity:--------

 sharedPreferences = EncryptedSharedPreferences.create(
        "secret_shared_prefs",
        masterKeyAlias,
        baseContext,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    ) as EncryptedSharedPreferences

在登录时执行此操作

  val editor = sharedPreferences.edit()
   editor.putString("EmailId", edtEmailId)
   editor.putString("password", edtPassword)
     editor.apply()

签入家庭活动:---------

checking in Homeactivity:---------

        val menu: Menu = bind.navRightView.getMenu()

    val nav_connection: MenuItem = menu.findItem(R.id.nav_login)

   val sharedPreference =
            applicationContext.getSharedPreferences("secret_shared_prefs", Context.MODE_PRIVATE)
    val value: String = sharedPreference.getString("EmailId", null).toString()

    if(!sharedPreference.contains(value))
    {
        nav_connection.title = "Loginn"

    }
    else{
        nav_connection.title = "Logout"

    }

该代码的

well输出始终具有标题为loginn的标题,这意味着它检测到的值为null需要帮助EncryptedSharedPreferences

well output of this code is always having a title as loginn that means it detecting value as nullneed help for EncryptedSharedPreferences thanks

推荐答案

您正在添加值,但未将其保存到 SharedPreferences .
您应该使用 commit() apply().

You are adding the values but not saving them to SharedPreferences.
You should be using commit() or apply().

执行此操作:

   val editor = sharedPreferences.edit()
   editor.putString("EmailId", edtEmailId)
   editor.putString("password", edtPassword)
   editor.apply() // Important

我还看到你在打电话

val sharedPreference = applicationContext.getSharedPreferences("secret_shared_prefs", Context.MODE_PRIVATE)`

可以给您正常的 SharedPreference .
您应该使用:

which gives you the normal SharedPreference, I guess.
You should be using:

val sharedPreferences = EncryptedSharedPreferences.create(
        "secret_shared_prefs",
        masterKeyAlias,
        baseContext,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    ) as EncryptedSharedPreferences


然后检查电子邮件地址是否为空.


Then check if the email address is empty or not.

if(!sharedPreference.contains("EmailId")
    nav_connection.title = "Loginn"
else
    nav_connection.title = "Logout"

这篇关于使用Android的EncryptedSharedPreferences检查值是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:14