android - 实现 SharedPreferences

标签 android sharedpreferences

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

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

一旦用户点击接受,我就不需要再次显示此 Activity 。

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

    };

}

最佳答案

引用下面的代码

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 {

 }

关于android - 实现 SharedPreferences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11734628/

相关文章:

android - 使用按钮中 SharedPreferences 中的字符串开始新 Activity

java - camera2 输出到位图

java - 如何在我的设置 Activity 中添加主题?

Android按钮背景采用原色

javascript - 如何在同一项目的android native和android phonegap中共享sqlite数据库

android - 如何在 SharedPreferences 中存储 Date 对象?

android - SharedPreferences 问题,覆盖

android - 首选项未使用 xml 中的默认值进行初始化

java - Android AES解密返回稀有字符

android - 如何避免过长的 switch-case 语句?