如何将用户名和密码存储在设备内存中。
即使用户关闭应用程序并返回,他也应该能够验证自己的用户名和密码。
现在我正在Eclipse中进行测试...所以请帮助我提供一些指针/链接,让我能够在Eclipse中进行测试,并最终在移动设备上运行。
我使用SharedPreferences来存储用户名/密码,它是一种更轻量级的存储解决方案,并且对您的应用程序安全。
嘿..我为您制作了一个简单的演示,用于保存用户名和密码..!!! 它存储在设备的内部存储器中, 它创建一个文件并通过/从该文件保存/提取数据.. 代码如下:
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends Activity {
LinearLayout lymain;
EditText user_edit;
EditText pass_edit;
TextView user_txt;
TextView pass_txt;
CheckBox savepass;
Button save;
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lymain = new LinearLayout(getApplicationContext());
lymain.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
lymain.setPadding(15,15,15,15);
lymain.setOrientation(1);
lymain.setGravity(Gravity.CENTER);
user_edit = new EditText(getApplicationContext());
user_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
pass_edit = new EditText(getApplicationContext());
pass_edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
user_txt = new TextView(getApplicationContext());
user_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
user_txt.setText("Enter Username");
pass_txt = new TextView(getApplicationContext());
pass_txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
pass_txt.setText("Enter Password");
savepass = new CheckBox(getApplicationContext());
savepass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,40));
savepass.setText("Save Username/Password?");
save = new Button(getApplicationContext());
save.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
save.setText(" SAVE ");
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String loginName = user_edit.getText().toString();
String password = pass_edit.getText().toString();
if(savepass.isChecked()){
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, loginName)
.putString(PREF_PASSWORD, password)
.commit();
Toast.makeText(getApplicationContext(),"Saved Successfully",Toast.LENGTH_LONG).show();
}
}
});
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
user_edit.setText(pref.getString(PREF_USERNAME, null));
pass_edit.setText(pref.getString(PREF_PASSWORD, null));
lymain.addView(user_txt);
lymain.addView(user_edit);
lymain.addView(pass_txt);
lymain.addView(pass_edit);
lymain.addView(savepass);
lymain.addView(save);
setContentView(lymain);
}
}