如何从另一个类的共享首选项中获取值

时间:2014-11-17 12:40:10

标签: android sharedpreferences

我想从我的共享偏好文件中获取一个字符串并用于更多课程,但我不知道为什么不工作。 我的读者课程是:

import android.app.Activity;
import android.content.SharedPreferences;

public class A {
    public static String url2;

    public void execute() {

        String URLPref = "URL";
        SharedPreferences prefs = getSharedPreferences("com.exam.search_preferences",Activity.MODE_PRIVATE);
        url2 = prefs.getString(URLPref , "");

    }

    private SharedPreferences getSharedPreferences(String string,
            int modePrivate) {

        return null;
    }


}

使用字符串的第二个类

public class SearchHome extends Activity {

    static String url2;
    A cls2= new A();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity);

        cls2.execute();
        url2 = A.url2;


        Toast.makeText(getApplicationContext(),"URL:" + url2 ,
        Toast.LENGTH_LONG).show();
...

抱歉我的英语不好,我从未学过。但我正在尝试!

5 个答案:

答案 0 :(得分:1)

如果您的数据不是保密的,那么如果您可以专门为共享首选项创建一个类并让其他活动访问它,则会更容易。你将节省大量的时间和代码将更容易跟进

public class HelperShared {
public static final String score = "Score";

public static final String tag_User_Machine = "tag_User_Machine",
    tag_Machine_Machine = "tag_Machine_Machine",
    tag_Draw_Machine = "tag_Draw_Machine",
    tag_Total_Machine = "tag_Total_Machine";


public static SharedPreferences preferences;

public static Editor editor;


public HelperShared(Context context) {
this.preferences = context.getSharedPreferences(score,
        Activity.MODE_PRIVATE);
this.editor = preferences.edit();

}


/*

* Getter and Setter methods for Machine

*/

public void setUserMachine(int UserMachine) {
editor.putInt(tag_User_Machine, UserMachine);
editor.commit();

}


public void setMachineMachine(int MachineMachine) {

   editor.putInt(tag_Machine_Machine, MachineMachine);
editor.commit();

}


public void setDrawMachine(int DrawMachine) {
editor.putInt(tag_Draw_Machine, DrawMachine);
editor.commit();

}


public void setTotalMachine(int TotalMachine) {
editor.putInt(tag_Total_Machine, TotalMachine);
editor.commit();

}


public int getUserMachine() {
return preferences.getInt(tag_User_Machine, 0);

}


public int getMachineMachine() {
return preferences.getInt(tag_Machine_Machine, 0);

}


public int getDrawMachine() {
return preferences.getInt(tag_Draw_Machine, 0);

}


public int getTotalMachine() {
return preferences.getInt(tag_Total_Machine, 0);


 }

}    

答案 1 :(得分:1)

您需要将Context传递给您的班级A,因为您可以从SharedPreferences对象获取Context。请注意,Activity在某种程度上是Context

public class A {
    public static String url2;

    /** @param context used to get the SharedPreferences */
    public void execute(Context context) {

        String URLPref = "URL";
        SharedPreferences prefs = context.getSharedPreferences("com.exam.search_preferences",Activity.MODE_PRIVATE);
        url2 = prefs.getString(URLPref , "");
    }
}

然后将Context传递给execute方法

public class SearchHome extends Activity {

    static String url2;
    A cls2= new A();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity);

        // pass context 'this' to the execute function
        // This works, because SearchHome extends Activity 
        cls2.execute(this);
        url2 = A.url2;
        ...

答案 2 :(得分:0)

 private SharedPreferences getSharedPreferences(String string,
            int modePrivate) {

        return null;
    }

问题在这里。

 return null;

您必须返回有效的SharedPreferences对象。否则你总会得到NullPointerException。

答案 3 :(得分:0)

当你想要一个pref:

时调用它
putPref("myKey", "mystring", getApplicationContext());

想要获得pref时调用此方法:

getPref("myKey", getApplicationContext());

您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。这些数据将在用户会话中持续存在(即使您的应用程序被终止)。

Different Modes:
1   MODE_APPEND
This will append the new preferences with the already exisiting preferences
2   MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set , it would enable write ahead logging by default
3   MODE_MULTI_PROCESS
This method will check for modification of preferences even if the sharedpreference instance has already been loaded
4   MODE_PRIVATE
By setting this mode , the file can only be accessed using calling application
5   MODE_WORLD_READABLE
This mode allow other application to read the preferences
6   MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences

Read More

答案 4 :(得分:0)

您只需要在要获取数据的类中创建共享prefrences object

SharedPreferences prefrences = getSharedPreferences("my prefs",MODE_PRIVATE)

Editor editor = prefrences.edit();

String s = edit.getString("your key",value);
希望它有所帮助!