【发布时间】:2018-11-26 13:53:15
【问题描述】:
我创建了一个必须设置设备时间的 Android 应用,因为设备无法记住时间。不知道如何设置设备的时间。
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy
HH:mm:ss");
String formattedDate = df.format(c);
SharedPreferences time = getSharedPreferences("PREFS",0);
Date Time = null;
try {
Time = df.parse(time.getString("time",""));
} catch (ParseException e) {
e.printStackTrace();
}
if (Time == null)
{
SharedPreferences.Editor editor = time.edit();
editor.putString("time", formattedDate);
editor.apply();
Toast.makeText(MainActivity.this, formattedDate,
Toast.LENGTH_SHORT).show();
}
else
{
try {
if (Time.before(df.parse(formattedDate)))
{
SharedPreferences.Editor editor = time.edit();
editor.putString("time", formattedDate);
editor.apply();
**** I want set device's time here from "time"
}
if (Time.after(df.parse(formattedDate)))
{
**** I want set device's time here from "time"
}
} catch (ParseException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
你能发布任何以前的试用代码吗?
-
感谢我已经编辑了我的问题。
-
迈克速度。我已经看到了该解决方案,但它对我不起作用。
标签: android