实现SharedPreferences

时间:2012-07-31 06:50:06

标签: android sharedpreferences

这是我的代码。  我有一个文本视图和2个按钮接受和拒绝。当用户单击“接受”按钮时,我使用共享首选项将状态保存为100。

下次用户登录时,我需要检查用户是否已经点击了接受按钮。  如果他已经接受了,那么我应该去家庭活动。

用户点击接受后,我不需要再次显示此活动。

public int kill;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Toast.makeText(Eula.this, "Status of the app is "+kill, Toast.LENGTH_LONG).show();
    if(kill==100)
    {
    Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("mobi.infoways.triviavs1_0","mobi.infoways.triviavs1_0.Home"));
        startActivity(intent);
    }
    setContentView(R.layout.eulatxt);
    Intent i2 = getIntent();                
    addListenerOnButton();
}
        private void addListenerOnButton() {

        TextView t = (TextView) findViewById(R.id.txtv1); 
        t.setText(f);
        Button Accept = (Button) findViewById(R.id.btn1);    
            Accept.setOnClickListener(startListener);
            Button Reject = (Button) findViewById(R.id.btn2);    
            Reject.setOnClickListener(startListener);
}

        OnClickListener startListener = new OnClickListener() 
        {
            public void onClick(View v) {

          switch (v.getId()) {
             case R.id.btn1:

                 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
                 SharedPreferences.Editor editor = prefs.edit();
                 editor.putInt("storedInt",100); 
                 editor.commit();
                     kill = prefs.getInt("storedInt", 100);
                    Toast.makeText(Eula.this, "status =" + kill, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName("mobi.infoways.triviavs1_0","mobi.infoways.triviavs1_0.Home"));
                startActivity(intent);
              break;
             case R.id.btn2:
                 Toast.makeText(Eula.this, "button 2 clicked", Toast.LENGTH_SHORT).show();
          Eula.this.finish();
              break;
          }  

    };

}

3 个答案:

答案 0 :(得分:1)

而不是

SharedPreferences prefs = getPreferences(MODE_PRIVATE);

使用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(YourAcitivyName.this);

onCreate()构造函数加载首选项并初始化super变量后,kill也在{{1}}。

在super.onCreate(savedInstanceState)旁边的onCreate方法中初始化kill;本身可能就够了..

答案 1 :(得分:1)

参考下面的代码

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Boolean checkforFirstTime;

sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();

checkforFirstTime = sharedPreferences.getBoolean("checkforFirstTime", true);

//Checking whether application is launched first time
if (checkforFirstTime) {

    //your code
    editor.putBoolean("checkforFirstTime", false);
    editor.commit();

 } else {

 }

答案 2 :(得分:0)

当加载共享的prefferences调用它时:

SharedPreferences save = getSharedPreferences("login", Activity.MODE_PRIVATE);
save.getInt(variable, 0); // zero is default value if you dont have it yet saved

保存时请拨打:

SharedPreferences settings = getSharedPreferences("login", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("storedInt", 100);
editor.commit();