从远程视图中的共享偏好中获取价值

Get value from shared preference in remoteview

我有一个小部件,我想从设置 activity 中设置一些值。 我已使用此代码将值保存到共享首选项:

MainActivity.editor.putInt("selected_theme", 1);
            MainActivity.editor.commit();

并且在远程视图中 class 我在 onUpdate 方法中这样做了:

MainActivity.prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE );
    MainActivity.editor = MainActivity.prefs.edit();

    int saved_value = MainActivity.prefs.getInt("selected_theme", 0);
    Log.d("ggg", "receiver: " + saved_value);

但是它总是给我值 0,这是默认值。我需要从已在 Activity class 中完成的共享首选项中获取 1,2,3.... 之类的整数值。 提前致谢:)

您应该始终使用 Utility Classes 来执行数据持久性(共享首选项、数据库、序列化等)等任务。
我在这里为您提供一个基本模板:

GenericUtility.class :

package com.your.packagename;

import android.content.Context;
import android.content.SharedPreferences;

public class GenericUtility {

    public static int getIntFromSharedPrefsForKey(String key, Context context)
    {
        int selectedValue = 0;

        SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
        selectedValue = prefs.getInt(key, 0);

        return selectedValue;
    }

    public static boolean setIntToSharedPrefsForKey(String key, int value, Context context)
    {
        boolean savedSuccessfully = false;

        SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        try
        {
            editor.putInt(key, value);
            editor.apply();
            savedSuccessfully = true;
        }
        catch (Exception e)
        {
            savedSuccessfully = false;
        }

        return savedSuccessfully;
    }

    public static String getStringFromSharedPrefsForKey(String key, Context context)
    {
        String selectedValue = "";

        SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
        selectedValue = prefs.getString(key, "");

        return selectedValue;
    }

    public static boolean setStringToSharedPrefsForKey(String key, String value, Context context)
    {
        boolean savedSuccessfully = false;

        SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        try
        {
            editor.putString(key, value);
            editor.apply();
            savedSuccessfully = true;
        }
        catch (Exception e)
        {
            savedSuccessfully = false;
        }

        return savedSuccessfully;
    }
}

用法示例:

用于在共享首选项中保存数据:

GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, getApplicationContext());

GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, MyActivity.this));

从共享首选项中检索数据:

int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", getApplicationContext());

int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", MyActivity.this);

希望对您有所帮助。