在Fragment中第一次调用时SharedPreferences为空

2024-05-07

我有一个示例 Android 应用程序,根据位置(邮政编码)和设置(SharedPreference)中设置的温度单位,该应用程序显示 7 天的天气。

当应用程序第一次获取温度并检查 SharedPreference 中设置的温度单位时,它似乎为空,并且 isMetric 设置为 TRUE。 Utility.isMetric 可以改进为表示没有从 SharedPreference 获取数据,但我的问题是为什么在 ForecastFragment 的 onCreateView 中首次调用 Utility.isMetric 时 SharedPreference 为空?

Utility.isMetric 访问 SharedPreference

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

预测片段调用Utility.isoMetric

boolean isMetric = Utility.isMetric(getActivity());

我有 logcat 显示这种行为,如果您想看到,请告诉我。

因为没有最大限制。字符,完整的代码可以访问https://github.com/gosaliajigar/CSC519/tree/master/CSC519-HW6 https://github.com/gosaliajigar/CSC519/tree/master/CSC519-HW6

预测片段

public class ForecastFragment extends Fragment implements LoaderCallbacks<Cursor> {

    private SimpleCursorAdapter mForecastAdapter;

    private static final int FORECAST_LOADER = 0;
    private String mLocation;

    // For the forecast view we're showing only a small subset of the stored data.
    // Specify the columns we need.
    private static final String[] FORECAST_COLUMNS = {
            // In this case the id needs to be fully qualified with a table name, since
            // the content provider joins the location & weather tables in the background
            // (both have an _id column)
            // On the one hand, that's annoying.  On the other, you can search the weather table
            // using the location set by the user, which is only in the Location table.
            // So the convenience is worth it.
            WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID,
            WeatherEntry.COLUMN_DATETEXT,
            WeatherEntry.COLUMN_SHORT_DESC,
            WeatherEntry.COLUMN_MAX_TEMP,
            WeatherEntry.COLUMN_MIN_TEMP,
            LocationEntry.COLUMN_LOCATION_SETTING
    };


    // These indices are tied to FORECAST_COLUMNS.  If FORECAST_COLUMNS changes, these
    // must change.
    public static final int COL_WEATHER_ID = 0;
    public static final int COL_WEATHER_DATE = 1;
    public static final int COL_WEATHER_DESC = 2;
    public static final int COL_WEATHER_MAX_TEMP = 3;
    public static final int COL_WEATHER_MIN_TEMP = 4;
    public static final int COL_LOCATION_SETTING = 5;

    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add this line in order for this fragment to handle menu events.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_refresh) {
            updateWeather();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final String[] columns = {WeatherEntry.COLUMN_DATETEXT,
                WeatherEntry.COLUMN_SHORT_DESC,
                WeatherEntry.COLUMN_MAX_TEMP,
                WeatherEntry.COLUMN_MIN_TEMP
        };

        final int[] viewIDs = {R.id.list_item_date_textview,
                R.id.list_item_forecast_textview,
                R.id.list_item_high_textview,
                R.id.list_item_low_textview
        };

        // The SimpleCursorAdapter will take data from the database through the
        // Loader and use it to populate the ListView it's attached to.
        mForecastAdapter = new SimpleCursorAdapter(
                getActivity(),
                R.layout.list_item_forecast,
                null,
                columns,
                viewIDs,
                0);
        mForecastAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                boolean isMetric = Utility.isMetric(getActivity());
                switch (columnIndex) {
                    case COL_WEATHER_MAX_TEMP: {
                        String high = Utility.formatTemperature(
                                cursor.getDouble(cursor.getColumnIndex(WeatherEntry.COLUMN_MAX_TEMP)), isMetric);
                        ((TextView) view).setText(high);
                        return true;
                    }
                    case COL_WEATHER_MIN_TEMP: {
                        // we have to do some formatting and possibly a conversion
                        String low = Utility.formatTemperature(
                                cursor.getDouble(cursor.getColumnIndex(WeatherEntry.COLUMN_MIN_TEMP)), isMetric);
                        ((TextView) view).setText(low);
                        return true;
                    }
                    case COL_WEATHER_DATE: {
                        String dateString = Utility.formatDate(
                                cursor.getString(cursor.getColumnIndex(WeatherEntry.COLUMN_DATETEXT)));
                        ((TextView) view).setText(dateString);
                        return true;
                    }
                }
                return false;
            }
        });

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Get a reference to the ListView, and attach this adapter to it.
        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(mForecastAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Cursor cursor = mForecastAdapter.getCursor();
                if (cursor != null && cursor.moveToPosition(position)) {
                    Intent intent = new Intent(getActivity(), DetailActivity.class)
                            .putExtra(DetailActivity.DATE_KEY, cursor.getString(COL_WEATHER_DATE));
                    startActivity(intent);
                }
            }
        });

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getLoaderManager().initLoader(FORECAST_LOADER, null, this);
        super.onActivityCreated(savedInstanceState);
    }

    private void updateWeather() {
        String location = Utility.getPreferredLocation(getActivity());
        // update weather only if location is not an EMPTY string
        if (location != null
                && location.length() > 0) {
            new FetchWeatherTask(getActivity()).execute(location, SettingsActivity.FORECAST_DAYS);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mLocation != null && !mLocation.equals(Utility.getPreferredLocation(getActivity()))) {
            getLoaderManager().restartLoader(FORECAST_LOADER, null, this);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // This is called when a new Loader needs to be created.  This
        // fragment only uses one loader, so we don't care about checking the id.

        // To only show current and future dates, get the String representation for today,
        // and filter the query to return weather only for dates after or including today.
        // Only return data after today.
        String startDate = WeatherContract.getDbDateString(new Date());

        // Sort order:  Ascending, by date.
        String sortOrder = WeatherEntry.COLUMN_DATETEXT + " ASC";

        mLocation = Utility.getPreferredLocation(getActivity());

        if (mLocation == null || mLocation.length() == 0) {
            mLocation = "00000";
        }

        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(mLocation, startDate);
        Log.d(getString(R.string.app_name), weatherForLocationUri.toString());

        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        return new CursorLoader(
                getActivity(),
                weatherForLocationUri,
                FORECAST_COLUMNS,
                null,
                null,
                sortOrder
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mForecastAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mForecastAdapter.swapCursor(null);
    }
}

Utility

public class Utility {
    public static String getPreferredLocation(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_location_key),
                context.getString(R.string.pref_location_default));
    }

    public static boolean isMetric(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(context.getString(R.string.pref_units_key),
                context.getString(R.string.pref_units_metric))
                .equals(context.getString(R.string.pref_units_metric));
    }

    static String formatTemperature(double temperature, boolean isMetric) {
        double temp;
        if ( !isMetric ) {
            temp = 9*temperature/5+32;
        } else {
            temp = temperature;
        }
        return String.format("%.0f", temp);
    }

    static String formatDate(String dateString) {
        Date date = WeatherContract.getDateFromDb(dateString);
        return DateFormat.getDateInstance().format(date);
    }
}

我已将 logcat 放入两个文件(Utility 和 ForecastFragment)中来演示该问题。这是安装应用程序时加载 ForecastFragment onCreateView 时第一次的 logcat。

07-05 17:35:57.562 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:35:57.563 21604-21604/com.example.android.weather.app D/JIGAR: isMetric true

这是按下刷新按钮后加载 ForecastFragment onCreateView 时的 logcat。

07-05 17:36:44.433 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.435 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.435 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.436 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.445 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.449 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.450 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.452 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.458 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.459 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.460 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.461 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.462 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.463 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.463 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}
07-05 17:36:44.464 21604-21604/com.example.android.weather.app D/JIGAR: {}

我现在已经从上述两个位置删除了 Log.d 语句,但非常欢迎您放置 log 语句。

注意:这是一个作业应用程序,文件中显示了待办事项及其完整的应用程序,但在使用该应用程序并理解代码时,我在应用程序中发现了这个错误,并试图理解它发生的原因。这是NOT作业或任何提交的一部分。


感谢 Dennis 和 Kaze,我的 Android 应用程序正在从 xml 文件加载默认共享首选项addPreferencesFromResource(R.xml.pref_general)在SettingActivity中extends PreferenceActivity这是一个单独的活动,当有人单击“设置”按钮时它就会启动,因此在按下“设置”按钮之前永远不会填充共享首选项。

您关于填充默认首选项的问题让我研究了它是如何填充的以及应该在 ForecastFragment 中填充它onCreate using PreferenceManager.setDefaultValues如下 ...

PreferenceManager.setDefaultValues(context, R.xml.pref_general, false);

这已经解决了我的问题。我将对你的答案进行投票!谢谢

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Fragment中第一次调用时SharedPreferences为空 的相关文章

  • minHeight 有什么作用吗?

    在附图中 我希望按钮列与图像的高度相匹配 但我也希望按钮列有一个最小高度 它正确匹配图像的高度 但不遵守 minHeight 并且会使按钮向下滑动 我正在为按钮列设置这些属性
  • 迁移到 java 17 后有关“每个进程的内存映射”和 JVM 崩溃的 GC 警告

    我们正在将 java 8 应用程序迁移到 java 17 并将 GC 从G1GC to ZGC 我们的应用程序作为容器运行 这两个基础映像之间的唯一区别是 java 的版本 例如对于 java 17 版本 FROM ubuntu 20 04
  • Clip 在 Java 中播放 WAV 文件时出现严重延迟

    我编写了一段代码来读取 WAV 文件 大小约为 80 mb 并播放该文件 问题是声音播放效果很差 极度滞后 你能告诉我有什么问题吗 这是我的代码 我称之为doPlayJframe 构造函数内的函数 private void doPlay f
  • ROOM迁移过程中如何处理索引信息

    CODE Entity tableName UserRepo indices Index value id unique true public class GitHubRepo PrimaryKey autoGenerate true p
  • 检查 Android 手机上的方向

    如何查看Android手机是横屏还是竖屏 当前配置用于确定要检索的资源 可从资源中获取Configuration object getResources getConfiguration orientation 您可以通过查看其值来检查方向
  • 反思 Groovy 脚本中声明的函数

    有没有一种方法可以获取 Groovy 脚本中声明的函数的反射数据 该脚本已通过GroovyShell目的 具体来说 我想枚举脚本中的函数并访问附加到它们的注释 Put this到 Groovy 脚本的最后一行 它将作为脚本的返回值 a la
  • Android Studio:无法启动守护进程

    当我尝试在 Android Studio 中导入 gradle 项目时 遇到以下错误 Unable to start the daemon process This problem might be caused by incorrect
  • 归并排序中的递归:两次递归调用

    private void mergesort int low int high line 1 if low lt high line 2 int middle low high 2 line 3 mergesort low middle l
  • 如何在 JFreeChart TimeSeries 图表上显示降雨指数和温度?

    目前 我的 TimeSeries 图表每 2 秒显示一个位置的温度 现在 如果我想每2秒显示一次降雨指数和温度 我该如何实现呢 这是我的代码 import testWeatherService TestWeatherTimeLapseSer
  • 调节麦克风录音音量

    我们正在尝试调整录音时的音量级别 麦克风似乎非常敏感 会接收到很多静电 我们查看了 setVolumeControlStream 但找不到传入其中来控制麦克风的流 将您的音频源设置为 MIC using MediaRecorder Audi
  • 尝试使用 Ruby Java Bridge (RJB) gem 时出现错误“无法创建 Java VM”

    我正在尝试实现 Ruby Java Bridge RJB gem 来与 JVM 通信 以便我可以运行 Open NLP gem 我在 Windows 8 上安装并运行了 Java 所有迹象 至少我所知道的 都表明 Java 已安装并可运行
  • Java直接内存:在自定义类中使用sun.misc.Cleaner

    在 Java 中 NIO 直接缓冲区分配的内存通过以下方式释放 sun misc Cleaner实例 一些比对象终结更有效的特殊幻像引用 这种清洁器机制是否仅针对直接缓冲区子类硬编码在 JVM 中 或者是否也可以在自定义组件中使用清洁器 例
  • 应用程序关闭时的倒计时问题

    我制作了一个 CountDownTimer 代码 我希望 CountDownTimer 在完成时重新启动 即使应用程序已关闭 但它仅在应用程序正在运行或重新启动应用程序时重新启动 因此 如果我在倒计时为 00 10 分钟 秒 时关闭应用程序
  • 将 JSON 参数从 java 发布到 sinatra 服务

    我有一个 Android 应用程序发布到我的 sinatra 服务 早些时候 我无法读取 sinatra 服务上的参数 但是 在我将内容类型设置为 x www form urlencoded 之后 我能够看到参数 但不完全是我想要的 我在
  • SharedFlow 和 StateFlow 的主要区别

    两者有什么区别共享流 and 状态流 以及如何使用这些MVI建筑学 使用简单更好吗Flow或者这些作为状态和事件 Flow 是冷的 意味着它仅在收集数据时才发出数据 另外Flow不能保存数据 可以把它看成是水在里面流动的管道 Flow中的数
  • 在webview android中加载本地html文件

    我正在尝试在 android 的 webview 中加载 html 文件的内容 但是 它给了我 网页不可用错误 如果我尝试使用谷歌或雅虎等网站 它们就会起作用 html文件位于src gt main gt assests gt index
  • 如何将图像从 Android 应用程序上传到网络服务器的特定文件夹中

    如何将图像从 android 移动到 Web 服务器上的指定文件夹 这是我的安卓代码 package com example bitmaptest import java io ByteArrayOutputStream import ja
  • java8 Collectors.toMap() 限制?

    我正在尝试使用java8Collectors toMap on a Stream of ZipEntry 这可能不是最好的想法 因为在处理过程中可能会发生异常 但我想这应该是可能的 我现在收到一个我不明白的编译错误 我猜是类型推理引擎 这是
  • Jackson 将单个项目反序列化到列表中

    我正在尝试使用一项服务 该服务为我提供了一个带有数组字段的实体 id 23233 items name item 1 name item 2 但是 当数组包含单个项目时 将返回该项目本身 而不是包含一个元素的数组 id 43567 item
  • Swagger/Openapi-Annotations:如何使用 $ref 生成 allOf?

    我正在生成 Rest 端点 包括添加OpenAPI Swagger对生成的代码进行注释 虽然它对于基本类型运行得很好 但我在自定义类方面遇到了一些问题 现在我有很多自定义类的重复架构条目 使用 Schema 实现 MyClass class

随机推荐

  • MSIE 和 addEventListener Javascript 中出现问题?

    document getElementById container addEventListener copy beforecopy false 在Chrome Safari中 上面将在复制页面内容时运行 beforecopy 函数 MSI
  • C# Null 传播运算符/条件访问表达式和 if 块

    The 空传播运算符 条件访问表达式 https roslyn codeplex com discussions 540883进来c 6 0 questions tagged c 23 6 0看起来是一个非常方便的功能 但我很好奇它是否有助
  • 在 Ruby 中创建 Microsoft Word (.docx) 文档

    有没有一种简单的方法可以在 Ruby 应用程序中创建 Word 文档 docx 实际上 就我而言 它是一个由 Linux 服务器提供服务的 Rails 应用程序 类似的宝石Prawn http prawn majesticseacreatu
  • 读取Android文件系统中的所有文件

    我正在编写一个Android mediaPlayer应用程序 所以我想扫描整个手机上的所有文件 即sd卡和手机内存 我可以从 SD 卡读取数据 但不能读取它的根目录 也就是说 我可以从路径中读取 sdcard folder 它工作得很好 但
  • ASP.NET 5 OAuth 不记名令牌身份验证

    我正在尝试在 ASP NET 5 中实现 OAuth 不记名令牌身份验证 并且很难找到如何执行此操作的示例 因为 OWIN 内容在 ASP NET 5 中已发生变化 例如IApplicationBuilder UseOAuthAuthori
  • 在组织内部分发我的 python 模块

    我用 python 制作了一些模块 我想将它们分发到我的组织内 这些模块已经存储在BitBucket中 例如 有什么方法可以使用 pip install 来分发它们吗 正确的方法是什么 您可以从 GitHub 进行 pip 安装 并且应该能
  • 在Lua中获取前一天的日期

    谁能告诉我如何使用 Lua 获取 YYYY MM DD 格式的前一天日期 即 一个片段 它将返回运行当天的前一天的日期 Try print os date Y m d os time 24 60 60 严格来说 这只能保证在 POSIX 系
  • R 从 .CSV 创建 NetCDF

    我正在尝试从 csv 文件创建 NetCDF 我在这里和其他地方读过一些教程 但仍然有一些疑问 我有一个这样的表 lat long time rh temp 41 109 6 1 1 40 107 18 2 2 39 105 6 3 3 4
  • 相当于 DB2 的 LIMIT

    你好吗LIMIT在 iSeries 版 DB2 中 我有一个包含超过 50 000 条记录的表 我想返回 0 到 10 000 条记录 以及 10 000 到 20 000 条记录 我知道你用 SQL 写的LIMIT 0 10000在 0
  • 如何在动态创建的一组 editText 上设置 onFocusChangeListener()?

    我有这段代码 每次前一个 lineaLayout 的 edittext 失去焦点时 我都会膨胀一个包含 3 个 editText 的 LinearLayout 我只想在最近创建的 editTexts 上使用 onFocusChangeLis
  • java中如何查找两个时间戳的差异?

    我有一个ArrayList包括多个时间戳 目的是找到第一个和最后一个元素的差异ArrayList String a ArrayList get 0 String b ArrayList get ArrayList size 1 long d
  • 具有最小长度的 TypeScript 数组

    如何在 TypeScript 中创建只接受具有两个或更多元素的数组的类型 needsTwoOrMore onlyOne should have error needsTwoOrMore one two should be allowed n
  • 使用 IntelliJ 的 Cucumber 找不到步骤定义

    我遇到了以下问题 我在 IntelliJ 中有四个 Cucumber 功能文件 我通过 IntelliJ 插件添加了 Cucumber 支持 创建功能后 我按如下方式编辑了配置 以便可以执行功能文件 Glue should be the n
  • 如何将 tkinter 窗口放在其他窗口之上?

    我正在使用 Python 2Tkinter and PyObjC 然后我用py2app 该程序工作正常 但每当我打开该程序时 该窗口都会以隐藏状态开始 因此直到我单击扩展坞上的图标将其调出时 它才会出现 有什么方法可以控制这个 使窗口位于应
  • 相当于 Rcpp 中的 'which' 函数

    我是 C 和 Rcpp 的新手 假设我有一个向量 t1 lt c 1 2 NA NA 3 4 1 NA 5 我想获得 t1 的元素索引NA 我可以写 NumericVector retIdxNA NumericVector x Step 1
  • Redhat Vim 中的可视化块插入

    我的 ec2 服务器附带了 redhat vim ec2 user vim version VIM Vi IMproved 7 2 2008 Aug 9 compiled Jul 7 2012 08 03 48 Included patch
  • 在 Java 中加载和缓存图像的最佳方法是什么?

    我有超过一千个 16 x 16 像素图块图像的大量集合 我在 Java 中制作的游戏需要这些图像 在不耗尽 JVM 可用内存的情况下存储切片的最佳方法是什么 我认为生成 1000 BufferedImages 可能并不明智 保持图像准备就绪
  • 如何默认显示带有手动(键盘)输入的时间选择器对话框?

    时间选择器对话框默认显示循环计时以选择日期和时间 相反 它需要默认显示键盘输入来选择日期和时间 在以圆形样式显示时间选择器对话框时 它具有键盘图标 可将圆形样式更改为手动输入样式 Android Oreo 操作系统设备可使用此功能 如何在支
  • 如何使用高复制数据存储

    好的 我已经看过了video http www google com events io 2011 sessions more 9s please under the covers of the high replication datas
  • 在Fragment中第一次调用时SharedPreferences为空

    我有一个示例 Android 应用程序 根据位置 邮政编码 和设置 SharedPreference 中设置的温度单位 该应用程序显示 7 天的天气 当应用程序第一次获取温度并检查 SharedPreference 中设置的温度单位时 它似