在我的项目中,我将提供一些可以使用会话和应用程序变量(如c#
. 我的信息是保密的,因此不能存储在像xml
提前致谢这样的文件中。
编辑:1-
我发现调用sharedpreference
但不知道如何使用的会话变量的解决方案请告诉我
- 如何初始化它和
- 从其他类设置它和
从其他人那里获得价值?
?...?...?..
首先,您的应用程序基本上随时都可能死掉,因为在您的应用程序在后台停留一段时间并且平台需要更多内存后,它可能会被 Android 调度程序杀死。
话虽如此,将数据存储在全局 Application 类中通常被认为有点 hacky。相反,您使用它的方式取决于您将如何访问应用程序。很多应用程序在 SharedPreferences 中保留了诸如 OAuth 令牌之类的东西,这是一件非常普遍接受的事情(尽管我不确定这到底有多洁净)。您可能会做的是将会话变量保存在 Application 类中,然后 - 每当应用程序死机时 - 只需重新进行身份验证。无论如何,这可能是一种很好的做法,因为在那之后可能有人拿起了电话,等等......
However, you seem to be under the impression that SharedPreferences can be read by anyone. This is incorrect, see this. Now, if you have a rooted phone, sure, then there's a way around that, but this is always going to be an issue, on a rooted phone you should basically consider that you don't really have any security at all...
Application
上课是为你准备的。使用它并保存您的应用程序级数据,如下所示:
public class WhatEverApp extends Application
{
String mApplicationLevelVar = "Hello";
}
WhatEverApp
将是您使用的应用程序的名称manifest.xml
在此处查看有关课程的详细讨论Application
。