Get value from shared preference in remoteview

434 views Asked by At

I am having a widget and i want to set some value from settings activity. I have saved values to shared pref using this code:

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

And in remoteview class i have done like this in onUpdate method:

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);

however it always gives me value 0, which is default. I need to get integer value like 1,2,3.... from shared preference which have been done in Activity class. Thanks in Advance :)

1

There are 1 answers

0
Salman Khakwani On BEST ANSWER

You should always use Utility Classes for performing tasks such as Data persistance (Shared Preferences , Data Base, Serialisation, etc).
Here i am providing you a basic Template:

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;
    }
}

Usage Example:

For Saving Data in Shared Preferences:

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

OR

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

For Retrieving Data From Shared Preferences:

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

OR

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

I hope this helps.