我有2项活动 1.Register.java 2.SecureXActivity.java 在Register活动中,我正在使用共享首选项注册我的应用程序。我使用共享首选项只显示一次Register活动。现在我的要求是 注册活动中的用户名将在注册时仅显示一次,并将其保存在SecureXActivity活动中,注册后此用户只能看到此活动。所以请在这方面帮助我?我是android的新手。
我的代码如下:
Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
public static final String USER_NAME = "USER";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 )
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras =getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("name");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(value);
}
}
我想要从一个活动到另一个活动的值,并且值应该保存到另一个活动。在上面的代码中,我使用intent完成了这个。但问题是价值仅在第二次活动中显示一次。按后退按钮或主页键时,值不可见?如何执行此操作。
为回应@Picarus而添加:
问题是我使用相同的共享首选项只显示一次注册活动。 但它没有在SecureX活动上显示名称。是否可以使用相同的共享首选项将值从注册活动传递到securex活动
下面再次给出整个代码 Register.java
public class Register extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
/*
* Check if we successfully logged in before.
* If we did, redirect to home page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged"))
{
Intent intent = new Intent(Register.this, SecureXActivity.class);
startActivity(intent);
}
Button b = (Button) findViewById(R.id.btnRegister);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText username = (EditText) findViewById(R.id.reg_employeeid);
EditText password = (EditText) findViewById(R.id.reg_password);
String welcm ="Welcome";
String space = " ";
EditText firstname = (EditText)findViewById(R.id.reg_firstname);
EditText lastname = (EditText)findViewById(R.id.reg_lastname);
String getfirstname = firstname.getText().toString();
String getlasttname = lastname.getText().toString();
String welcome=welcm.concat(space).concat(getfirstname).concat(space).concat(getlasttname);
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 )
{
if(username.getText().toString().equals("test") && password.getText().toString().equals("test"))
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", welcome);
editor.commit();
Intent intent = new Intent(Register.this, SecureXActivity.class);
//intent.putExtra("name", welcome);
startActivity(intent);
}
}
}
});
}
}
SecureXActivity.java
public class SecureXActivity extends Activity
{
public static final String PREFS_NAME = "LoginPrefs";
private ListView m_listview;
Button btnSendlogs;
Button btnMailadmin;
Button btnContactSupport;
Button btnSettings;
@Override
public void onCreate(Bundle savedInstanceState)
{
Thread.setDefaultUncaughtExceptionHandler(new exceptionHandler(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Here we are setting the installation date of a application
ContextWrapper context = this;
PackageManager packageManager = context.getPackageManager();
ApplicationInfo appInfo;
try
{
appInfo = packageManager.getApplicationInfo("com.simsys.securex", 0);
String sourceDir= appInfo.sourceDir;
long dateTimeSync= new File(sourceDir).lastModified();
String DateTimeStamp=getDate(dateTimeSync, "dd/MM/yyyy hh:mm:ss");
String dateTimeSync2="Installed_Date:";
String dateTimeSync3=dateTimeSync2.concat(DateTimeStamp);
TextView txtCurrentTime= (TextView)findViewById(R.id.textViewdatetime);
txtCurrentTime.setText(dateTimeSync3);
}
catch (NameNotFoundException e)
{
e.printStackTrace();
}
//Here We Welcome the user
//List View
m_listview = (ListView) findViewById(R.id.list_view);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("CAMERA");
listItems.add("RECORDER");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
m_listview.setAdapter(adapter);
if(isBluetoothServiceRunning())
{
listItems.add("BLUETOOTH");
}
if(isGprsServiceRunning())
{
listItems.add("GPRS");
}
if(isWifiServiceRunning())
{
listItems.add("WIFI");
}
adapter.notifyDataSetChanged();
//Call to Buttons Functionality
btnSendlogs = (Button) findViewById(R.id.button_sendlogs);
btnSendlogs.setOnClickListener(myhandler1);
btnMailadmin=(Button) findViewById(R.id.button_mailtoadmin);
btnMailadmin.setOnClickListener(MailadminHandler);
btnContactSupport = (Button) findViewById(R.id.button_contactsupport);
btnContactSupport.setOnClickListener(ContactSupportHandler);
}
//Navigation from Registration to Main page
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater Inflater = getMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.logout)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String data2=settings.getString("name", "");
TextView txtwelcm= (TextView)findViewById(R.id.textViewwelcm);
txtwelcm.setText(data2);
SharedPreferences.Editor editor = settings.edit();
editor.remove("name");
editor.commit();
finish();
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:1)
为了实现sharedPreferences,请使用以下代码
Sharedpreferences myPref = this.getSharedPreferneces("your_file_name",MODE);
SharedPreferences.Editor editor = myPref.edit();
editor.putString("String_Name",value);
editor.putInt("Int_Name","value");
从SharedPreferences检索
Sharedpreferences myPref = this.getSharedPreferneces("your_file_name",MODE);
String stringValue = myPref.getStirng("String_Name","DEFAULT_VALUE");
答案 1 :(得分:0)
您可以从任何活动(SecureXActivity)中检索您的SharedPreferences,就像在Register.java中执行它一样
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
settings.getString("logged", "");
你尝试过吗?你找到了什么问题?
编辑:可能是因为你试图从onCreate加载它。刷新或单击主页时不会调用此方法。您应该在应用中找到更合适的点。