android下保存用户账号密码和读取账号密码

这篇博客介绍了如何在Android应用中使用SharedPreferences来安全地保存用户账号和密码,提供了一种业务方法实现,该方法可以成功或失败地保存用户信息,并通过SharedPreferences进行数据读取。
private EditText mima;
    private EditText zhanghao;
    private Button button;
    private CheckBox checkbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        zhanghao=(EditText)findViewById(R.id.zhanghao);
        mima=(EditText)findViewById(R.id.mima);
        checkbox=(CheckBox)findViewById(R.id.checkBox);
        Map<String,String> map=services.servicekey(this);
        if(map!=null){
            zhanghao.setText(map.get("zh"));
            mima.setText(map.get("ma"));
            
        }
        
        
    }
    public void longs(View view){
        String zh=zhanghao.getText().toString().trim();
        String ma=mima.getText().toString().trim();
        if(TextUtils.isEmpty(zh)||TextUtils.isEmpty(ma)){
            Toast.makeText(getApplicationContext(), "密码或账号不能为空", 0).show();
        
        }else{//判断是否保存密码
            if(checkbox.isChecked()){
                //保存密码
                boolean bb =services.serviceText(zh, ma, this);
                if(bb){
                    Toast.makeText(getApplicationContext(), "保存成功", 0).show();
                }
            }
            //登录发送消息到服务器 验证密码是否正确
            if("hejun".equals(zh)&&"4834591".equals(ma)){
                Toast.makeText(getApplicationContext(), "登录成功",0).show();
                
            }else{
                Toast.makeText(getApplicationContext(), "密码错误", 0).show();
                
            }
            
        }
        
        
    }

业务方法


 //保存用户名 密码的业务方法
    // true保存成功 false保存失败

public static boolean serviceText(String zh ,String ma ,Context context){
		
		try {
			File file =new File(context.getFilesDir(),"info.txt");
			FileOutputStream fos =new FileOutputStream(file);
			fos.write((zh+"##"+ma).getBytes());
			fos.close();
			return true;
			
		} catch (Exception e) {
			
			e.printStackTrace();
			return false;
		}
		
		
		
	}
	//获取保存的数据
	public static Map<String, String >servicekey(Context context){
		File file =new File(context.getFilesDir(),"info.txt");
		try {
			FileInputStream fis =new FileInputStream(file);
			BufferedReader buffer =new BufferedReader(new InputStreamReader(fis));
			String str =buffer.readLine();
			String[] infos=str.split("##");
			Map<String,String> map=new HashMap<String, String>();
		    map.put("zh",infos[0] );
		    map.put("ma", infos[1]);
		    return map;
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}		
		
		
	}
}

利用sharedPreference读取和保存数据

public static void SharedPerfer(Context context,String name,String word){
          SharedPreferences sp =context.getSharedPreferences("shared", context.MODE_PRIVATE);
           Editor edit= sp.edit();
           edit.putString("name", name);
           edit.putString("word", word);
           edit.commit();
           

   SharedPreferences sp= getSharedPreferences("shared", MODE_PRIVATE);
		 String zh=  sp.getString("zh", "");
		 zh.setText(zh);
		  String ma sp.getString("ma", "");
		ma.setText(ma);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值