android中的持久性/共享存储
嗨,我正在尝试为持久/共享首选项编写代码。我正在成功地将数据保存到持久性存储中。但每次我启动应用程序时,它都会带我进入存储活动。我有两个活动主要活动和第二个活动。在主活动中,我保存数据,在第二个活动中,我显示存储的数据。这里我想要的是,当数据第一次存储时,它应该直接移动到第二个活动。我想实现一些类似watsapp注册过程的功能,你第一次输入你的号码和pin,在你卸载应用程序或手动清除数据之前,它不会要求你输入。我怎么能这样做呢。下面是我尝试过的代码android中的持久性/共享存储,android,sharedpreferences,persistent-storage,Android,Sharedpreferences,Persistent Storage,嗨,我正在尝试为持久/共享首选项编写代码。我正在成功地将数据保存到持久性存储中。但每次我启动应用程序时,它都会带我进入存储活动。我有两个活动主要活动和第二个活动。在主活动中,我保存数据,在第二个活动中,我显示存储的数据。这里我想要的是,当数据第一次存储时,它应该直接移动到第二个活动。我想实现一些类似watsapp注册过程的功能,你第一次输入你的号码和pin,在你卸载应用程序或手动清除数据之前,它不会要求你输入。我怎么能这样做呢。下面是我尝试过的代码 public class MainActivi
public class MainActivity extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int id = settings.getInt("PREFS_NAME", 0);
if (id > 0) {
Intent second = new Intent(getApplicationContext(), seond.class);
startActivity(second);
}
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText email = (EditText) findViewById(R.id.editText2);
Button btn = (Button) findViewById(R.id.btnsaveprefs);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("MSISDN", name.getText().toString());
editor.putString("PIN", email.getText().toString());
editor.commit();
Intent second = new Intent(getApplicationContext(), seond.class);
startActivity(second);
}
});
}
}
我的第二项活动是
public class seond extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tvname = (TextView) findViewById(R.id.uname);
TextView tvemail = (TextView) findViewById(R.id.uemail);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
tvname.setText(settings.getString("MSISDN", "Unknown"));
tvemail.setText(settings.getString("PIN", "Email default"));
}
}
我认为你的问题在于:
int id = settings.getInt("PREFS_NAME", 0);
您获取“PREFS_NAME”的值,然后检查它是否为
>0
,但从不为键PREFS_NAME
存储值。我猜你想在这里使用PIN
或MSISDN
?这是正确的方法:
public class MainActivity extends Activity {
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int id = settings.getInt("PREFS_NAME", 0);
if (id > 0) {
Intent second = new Intent(getApplicationContext(), Second.class);
startActivity(second);
}
final EditText name = (EditText) findViewById(R.id.editText1);
final EditText email = (EditText) findViewById(R.id.editText2);
Button btn = (Button) findViewById(R.id.btnsaveprefs);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = settings.edit();
editor.putString("MSISDN", name.getText().toString());
editor.putString("PIN", email.getText().toString());
editors.putInt("PREFS_NAME", 1); // id = 1
editor.commit();
Intent second=new Intent(getApplicationContext(), Second.class);
startActivity(second);
}
});
}
}
public class Second extends Activity { // classes begin with CAPITAL
public static final String PREFS_NAME = "MSISDN and PIN";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tvname = (TextView) findViewById(R.id.uname);
TextView tvemail = (TextView) findViewById(R.id.uemail);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
tvname.setText(settings.getString("MSISDN", "Unknown"));
tvemail.setText(settings.getString("PIN", "Email default"));
}
}
请注意,您没有按照其他答案中所述设置id。顺便说一下,你的编码风格很糟糕:永远不要用小写字母开头类名,为什么
id
是int?您可以将其用作布尔值。为什么要在名为PIN
的首选项中保存电子邮件,并在名为MSISDN
的首选项中保存名称?您在哪里设置要检查的名称首选项condition@niteshgoel check编辑的问题您在哪里设置PREFS_NAME?学习设置代码格式,不要在问题中发布不相关的代码不确定错在哪里?我正在努力,但无法修复它。