使用 getSharedPreferences 获取时间
我是 Java 和 Android 开发的完全初学者,并且有一个关于偏好的问题。
一个教程要求我使用首选项来读取并节省调用 onCreate 方法的时间。
我已经在这个问题上思考了至少两天,但一无所获。谁能给我一个关于如何编写此类操作的示例?
public static final String PREFS_NAME = "Time_Pref";
public class QuizSplashActivity extends QuizActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences = getSharedPreferences ("Time_Pref", "MODE_PRIVATE);
}
但这就是我似乎迷失的地方。我什至不确定我是否正朝着正确的方向前进。
I'm a complete beginner to Java and Android development and had a question about preferences.
A tutorial asked me to use preferences to read and save the time in which the onCreate method is called.
I've sat on this problem for at least two days but have come up with nothing. Can anyone give me an example on how to code such an action?
public static final String PREFS_NAME = "Time_Pref";
public class QuizSplashActivity extends QuizActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences = getSharedPreferences ("Time_Pref", "MODE_PRIVATE);
}
But that's where I seem to get lost. I'm not even sure if I'm heading in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
虽然我倾向于同意 Falmarri 对您的问题的评论,但我将提供一些指示...
在 onCreate() 中您应该做的第一件事是使用
new 创建
将其初始化为当前日期/时间。GregorianCalendar
的实例GregorianCalendar()接下来使用
getSharedPreferences
创建您的SharedPreferences
,然后使用其edit()
方法获取编辑器。存储时间的最简单方法是以毫秒为单位,因此请使用 GregorianCalendar
getTimeInMillis
方法(继承自 Calendar)来获取Long
结果。最后,使用 SharedPreferences 编辑器的
putLong()
方法存储getTimeInMillis
的结果,并使用编辑器的commit()
方法使更改永久生效。搜索这些页面将为您提供所需的所有信息...
Android 开发者
Although I tend to agree with Falmarri's comment on your question, I'll provide some pointers...
The first thing you should do in onCreate() is to create an instance of
GregorianCalendar
usingnew GregorianCalendar()
to initialize it to current date/time.Next use
getSharedPreferences
to create yourSharedPreferences
then get an editor using itsedit()
method.The simplest way to store the time would be in milliseconds so use the GregorianCalendar
getTimeInMillis
method (inherited from Calendar) to get aLong
result.Finally, use the SharedPreferences editor's
putLong()
method to store the result ofgetTimeInMillis
and use the editor'scommit()
method to make the change permanent.Searching these pages will give you all the information you need...
Android Developers
我建议您阅读开发指南中有关 SharedPreferences 的章节。您可以在此处找到它。
我建议您使用 System.currentTimeMillis() 来获取要保存的时间值。
将某些内容保存到首选项的基本代码应如下所示:
然后使用 android.text.format.Time 类在从首选项读取时间后格式化该时间:
读取 Time 类 文档 了解如何使用其方法格式化/转换时间:)
I'd recommend you to read the dev guide's chapter about SharedPreferences. You can find it here.
I suggest you to use System.currentTimeMillis() to get the time value to save.
Basic code to save something to preferences should look like this:
Then use android.text.format.Time class to format that time after reading it from preferences:
Read the Time class documentation to learn how to format/convert time with its methods :)