未检索保存的首选项
Saved Preference not getting retrieved
我的项目中有一个视频下载器 class 从给定的 url 下载视频并在下载完成后将首选项保存为 true。我在另一个 class 中检查首选项是否为真,以从其路径获取视频进行播放。下载器首先检查文件是否下载如下:
string isVideoDownloaded = Utils.readPreferences(ctx, video.getUrl(), "false");
bool isVideoAvailable = Boolean.Parse(isVideoDownloaded);
下载完成后,执行以下代码。
activity.RunOnUiThread(() =>
{
Utils.savePreferences(ctx, video.getUrl(), "true");
});
首选项按以下方式保存:
public static void savePreferences(Context activity, string key, string defaultValue)
{
ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(activity.ApplicationContext);
ISharedPreferencesEditor editor = sp.Edit();
editor.Clear();
editor.PutString(key, defaultValue);
editor.Commit();
}
在播放视频之前,通过以下方法检查首选项:
private bool isVideoDownloaded(Video video)
{
string isVideoDownloaded = Utils.readPreferences(context, video.getUrl(), "false");
bool isVideoAvailable = Boolean.Parse(isVideoDownloaded);
if (isVideoAvailable)
{
//If video is downloaded then hide its progress
hideProgressSpinner(video);
return true;
}
showProgressSpinner(video);
return false;
}
但首选项始终返回为 false。我使用相同的上下文作为下载的回报。这段代码在我第一次编写时有效,但现在每次都 returns false。
我是 android 的初学者,所以我很难弄清楚我在这里做错了什么以及如何解决它。
我该怎么做才能让它发挥作用?感谢任何帮助。
在 savePerference
方法中删除 editor.Clear()
public static void savePreferences(Context activity, string key, string defaultValue)
{
ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(activity.ApplicationContext);
ISharedPreferencesEditor editor = sp.Edit();
// editor.Clear(); comment this line of code
editor.PutString(key, defaultValue);
editor.Commit();
}
我的项目中有一个视频下载器 class 从给定的 url 下载视频并在下载完成后将首选项保存为 true。我在另一个 class 中检查首选项是否为真,以从其路径获取视频进行播放。下载器首先检查文件是否下载如下:
string isVideoDownloaded = Utils.readPreferences(ctx, video.getUrl(), "false");
bool isVideoAvailable = Boolean.Parse(isVideoDownloaded);
下载完成后,执行以下代码。
activity.RunOnUiThread(() =>
{
Utils.savePreferences(ctx, video.getUrl(), "true");
});
首选项按以下方式保存:
public static void savePreferences(Context activity, string key, string defaultValue)
{
ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(activity.ApplicationContext);
ISharedPreferencesEditor editor = sp.Edit();
editor.Clear();
editor.PutString(key, defaultValue);
editor.Commit();
}
在播放视频之前,通过以下方法检查首选项:
private bool isVideoDownloaded(Video video)
{
string isVideoDownloaded = Utils.readPreferences(context, video.getUrl(), "false");
bool isVideoAvailable = Boolean.Parse(isVideoDownloaded);
if (isVideoAvailable)
{
//If video is downloaded then hide its progress
hideProgressSpinner(video);
return true;
}
showProgressSpinner(video);
return false;
}
但首选项始终返回为 false。我使用相同的上下文作为下载的回报。这段代码在我第一次编写时有效,但现在每次都 returns false。 我是 android 的初学者,所以我很难弄清楚我在这里做错了什么以及如何解决它。
我该怎么做才能让它发挥作用?感谢任何帮助。
在 savePerference
方法中删除 editor.Clear()
public static void savePreferences(Context activity, string key, string defaultValue)
{
ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(activity.ApplicationContext);
ISharedPreferencesEditor editor = sp.Edit();
// editor.Clear(); comment this line of code
editor.PutString(key, defaultValue);
editor.Commit();
}