Android no activity getSharedPreferences

I have a problem reading getSharedPreferences from the non-Activity class to set a playlist in the player ... In my work, I take a string variable from edittext to get the path to the folder for working 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 action, I can change and save the String value in the general settings ... But ho, can I do this from an open class ...? Please rate any examples ...

+5
source share
4 answers

One way is to use the Application object . This is a dirty hack, but nonetheless sometimes useful.

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

, , , .

MyApplication.getInstance(), .

MyApplication .

+8

, :

MainActivity:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Test t = new Test("Show Preference: ");
        getSharedPreferences("PATH", MODE_PRIVATE)
        .edit()
        .putString("p", "preference")
        .commit();
        t.showToast(this.getApplicationContext());
    }
}

TestClass:

public class Test {
    private String s;

    public Test(String s){
        this.s = s;
    }

    public void showToast (Context c){
        String pref = c.getSharedPreferences("PATH", Context.MODE_PRIVATE).getString("p", "Error");
        Toast.makeText(c.getApplicationContext(), s + pref, Toast.LENGTH_LONG).show();
    }
}

+8

,   , ,

           public class xyx{
            public  xyx(Context context){
             this.context= context
             }

     Now use this context 
+1

MainActivity.this. getSharedPreferences

0

All Articles