Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android non-Activity getSharedPreferences

I have a problem by reading getSharedPreferences from non-Activity class to set playlist in player... In my Activity I take string variable from edittext to get path of folder to work with audio files...

    public class MainActivity extends Activity {


String ppp;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
    if (PATH == null){
        ..........
            ...........
        path_tv.setText("folder is undefined");
    }
    else {
        path_tv.setText("folder defined: /mnt/sdcard/" + PATH);
    }
    set_path.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (path_edit.getText().toString().length() == 0){
                Toast.makeText(getBaseContext(), "folder is undefined", Toast.LENGTH_SHORT).show();
            }
            else {
            ppp = path_edit.getText().toString();
            getSharedPreferences("PATH", MODE_PRIVATE)
            .edit()
            .putString("path", ppp)
            .commit();
            File folder = new File(Environment.getExternalStorageDirectory() + "/" + ppp);
            boolean success = false;
            if (!folder.exists()) {
                success = folder.mkdir();
                if (success) Toast.makeText(getBaseContext(), ".....", Toast.LENGTH_SHORT).show();
            }
            String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
            path_tv.setText("........ /mnt/sdcard/" + PATH);
            path_edit.setText("");
            }
        }
    });

So, in activity I can change and save value of String in shared preferences... But ho can I do it from public class...? Appreciate any examples...

like image 774
timonvlad Avatar asked Sep 13 '12 11:09

timonvlad


People also ask

What is the difference between apply () and commit () on the editor?

Use apply(). It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file. Save this answer.

Where are Android shared preferences stored?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .

What is the difference between commit () and apply () method in SharedPreferences ()?

We can call commit() or apply() to save the values in the SharedPreferences file. The commit() saves the values immediately whereas apply() saves the values asynchronously.

How to use shared preference in Android?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.


1 Answers

One way is to use Application object. This is a dirty hack but none the less sometimes helpful.

First you need a static member in your Application class, so:

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static MyApplication getInstance() {
        return instance;
    }
}

Since Application object is always created before any activity is created and run and is kept throughout application lifetime you can always be sure you will have proper one.

Then in your code simply call MyApplication.getInstance() and you will have global app context.

Remember to declare MyApplication in manifest.

like image 184
Marcin Gil Avatar answered Oct 05 '22 12:10

Marcin Gil