Android应用中用户名密码sd存储sp存储rom存储
2013-06-03 19:36
573 查看
public class LoginActivity extends Activity { // 声明 获取的用户名与密码的组件 public EditText edit_name, edit_pass; // 声明登陆按钮对象 public Button btn_login; // 声明CheckBox组件对象 public CheckBox box_remember; // 创建业务对象 public FileService fileService; // 声明 获取单选组件 public RadioButton radio_rom, radio_sp, radio_sd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置显示视图 setContentView(R.layout.activity_login); // 实例化业务对象 fileService = new FileService(this); // 根据id名称获取相应组件对象 edit_name = (EditText) findViewById(R.id.name_value); edit_pass = (EditText) findViewById(R.id.pass_value); btn_login = (Button) findViewById(R.id.but); box_remember = (CheckBox) findViewById(R.id.cobx); // 给按钮注册事件 btn_login.setOnClickListener(new MyOnClickListener()); radio_rom = (RadioButton) findViewById(R.id.radio_rom); radio_sp = (RadioButton) findViewById(R.id.radio_sp); radio_sd = (RadioButton) findViewById(R.id.radio_sd); // 实现数据返回 SharedPreferences preferences = this.getSharedPreferences("csdn", Context.MODE_PRIVATE); edit_name.setText(preferences.getString("name", "csdn")); edit_pass.setText(preferences.getString("pass", "csdn")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login, menu); return true; } // 内部类 class MyOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.but: // 获取用户名与密码 String name = edit_name.getText().toString(); String pass = edit_pass.getText().toString(); // 判断用户名与密码是否为空 if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) { Toast.makeText(LoginActivity.this, "用户名或者密码不能为空", Toast.LENGTH_LONG).show(); return; } else { // 判断是否记住密码 if (box_remember.isChecked()) { // 判断采用什么方式保存 if (radio_rom.isChecked()) { // 采用rom保存 } else if (radio_sp.isChecked()) { // 采用sp保存 boolean flag = fileService.saveBysp(name, pass, "csdn"); if (flag) { Toast.makeText(LoginActivity.this, "保存成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(LoginActivity.this, "保存失败", Toast.LENGTH_LONG).show(); } } else if (radio_sd.isChecked()) { // 采用sd保存 } } } break; default: break; } } } } ------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------- public class FileService { //上下方对象 public Context context; public FileService(Context context){ this.context = context; } /** * 住手机内存卡上存储 用户名与密码的操作 * * */ public boolean saveToRom(String name,String pass,String fileName){ //上下文对象的api try { //通过 openFileOutput()方法获取一个文件 的输出流对象 FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); //拼接用户名与密码 String result = name + ":" +pass; //写入 fos.write(result.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } //读取数据操作 public Map<String, String> readFile(String fileName){ Map<String ,String> map = null; try { FileInputStream fis = context.openFileInput(fileName); String value = StreanTools.getValue(fis); String values[] = value.split(":"); if(values.length >0){ map = new HashMap<String, String>(); map.put("name", values[0]); map.put("pass", values[1]); } } catch (Exception e) { e.printStackTrace(); } return map; } /** * 采用SharedPreferences * @param name * @param pass * @param fileName * @return */ public boolean saveBysp(String name,String pass,String fileName){ //通过上下文的api 获取 SharedPreferences对象 SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE); //根据SharedPreferences对象的edit 方法 返回 editor对象 Editor editor = preferences.edit(); //存放数据 editor.putString("name", name); editor.putString("pass", pass); editor.putInt("age", 8); editor.putBoolean("flag", true); //提交 return editor.commit(); } } ------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------- public class StreanTools { public static String getValue(FileInputStream fis)throws Exception{ //字节 流输出流对象 ByteArrayOutputStream stream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = -1; while((length = fis.read(buffer)) != -1){ stream.write(buffer, 0, length); } stream.flush(); stream.close(); String value = stream.toString(); return value; } }
相关文章推荐
- Android_rom存储,sp存储,sdcard存储
- Android学习(19) -- 数据存储之File (简单登录保存用户名和密码)
- android 保存 用户名和密码 设置等应用信息优化
- android 保存 用户名和密码 设置等应用信息优化
- Android 往手机内存卡上存储用户名与密码的操作
- Android 保存 用户名和密码 设置等应用信息优化
- Android数据存储之SharedPreferences的简单使用(保存用户名和密码)
- 应用数据存储到sdcard上一定要规范,android4.4.2有新规范
- Android实战教程第七篇之如何在内存中存储用户名和密码
- Android简易实战教程--第七话《在内存中存储用户名和密码》
- Android应用安全实践 -- 某些Android应用在客户端明文存储登录密码
- Android简易实战教程--第七话《在内存中存储用户名和密码》
- Android系统信息获取 之一:系统存储信息的获取(RAM,ROM,SDCard)
- Android系统信息获取 之一:系统存储信息的获取(RAM,ROM,SDCard)
- android应用开发存储方式之SQLite(android自带的数据库)的一些个人理解
- Android保存用户名和密码
- Android sd相关笔记 - Android4.4上应用写sd卡问题
- Android——用户登陆及用户名和密码的保存
- Android应用开发SharedPreferences存储数据的使用方法
- ionic开发——利用localStorage存储用户名和密码