android 登录开发,Android开发之自己主动登录功能的实现

这篇博客主要介绍了如何使用SharedPreferences实现Android应用的登录状态持久化。当用户登录后,通过SharedPreferences存储登录成功的标志,以便下次打开应用时直接跳转到主界面,无需再次输入密码。代码示例展示了如何读取和写入SharedPreferences,以及如何从SQLite数据库验证用户账号和密码。
摘要由CSDN通过智能技术生成

在我们平时使用的手机应用都能够实现仅仅须要登陆一次账号后,第二次进入应用直接跳转到效果界面的效果,还有QQ的登陆框是怎样记忆我们的隐身登陆,保存账号选项的呢,这些都是通过使用SharedPreferences共享參数效果实现的,而无须使用数据库来存储。下面我们直接看具体代码分析。

package com.example.account.login;

import java.util.HashMap;

import java.util.Map;

import com.android.dao.MySQLiteOpenHelper;

import com.example.account.MainActivity;

import com.example.account.R;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class LoginActivity extends Activity {

private EditText e1, e2;

private SQLiteOpenHelper helper;

private boolean flag, flag2, flag3;

private HashMap map;

@SuppressWarnings("unchecked")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.login);

TextView textView = (TextView) this.findViewById(R.id.textView1);

e1 = (EditText) this.findViewById(R.id.editText1);

e2 = (EditText) this.findViewById(R.id.editText2);

//从共享參数获取数据

map = (HashMap) getMsg("login");

if (map != null && !map.isEmpty()) {

if ((Boolean) map.get("login2")) {

//若值为true,用户无需输入password,直接跳转进入操作界面

Intent intent = new Intent(LoginActivity.this,

MainActivity.class);

startActivity(intent);

}

}

helper = new MySQLiteOpenHelper(this);

textView.setText("登录界面");

Button button = (Button) findViewById(R.id.button2);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if (!e1.getText().toString().isEmpty()

&& !e2.getText().toString().isEmpty()) {

//从数据库获取账号信息

SQLiteDatabase database = helper.getReadableDatabase();

Cursor cursor = database.query("user", new String[] {

"username", "password" }, null, null, null, null,

null);

while (cursor.moveToNext()) {

flag = e1

.getText()

.toString()

.equals(cursor.getString(cursor

.getColumnIndex("username")));

flag2 = e2

.getText()

.toString()

.equals(cursor.getString(cursor

.getColumnIndex("password")));

if (flag && flag2) {

Intent intent = new Intent(LoginActivity.this,

MainActivity.class);

startActivity(intent);

//登陆跳转动画

overridePendingTransition(R.anim.zoomin,

R.anim.zoomout);

Toast.makeText(LoginActivity.this, "登录成功",

Toast.LENGTH_SHORT).show();

flag3 = true;

//登陆成功后将flag设置为ture存入共享參数中

HashMap map = new HashMap();

map.put("login2", flag3);

saveMsg("login", map);

}

}

if (!flag3) {

Toast.makeText(LoginActivity.this, "您输入的帐号或password有误",

Toast.LENGTH_SHORT).show();

}

} else {

Toast.makeText(LoginActivity.this, "请正确输入您的帐号password",

Toast.LENGTH_SHORT).show();

}

}

});

Button button2 = (Button) findViewById(R.id.button1);

button2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(LoginActivity.this,

RegisterActivity.class);

startActivity(intent);

}

});

}

//将数据存储进入共享參数

public boolean saveMsg(String fileName, Map map) {

boolean flag = false;

// 一般Mode都使用private,比較安全

SharedPreferences preferences = getSharedPreferences(fileName,

Context.MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

// Map类提供了一个称为entrySet()的方法,这种方法返回一个Map.Entry实例化后的对象集。

// 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,

// 因此,上面的代码能够被组织得更符合逻辑

for (Map.Entry entry : map.entrySet()) {

String key = entry.getKey();

Object object = entry.getValue();

// 依据值得不同类型,加入

if (object instanceof Boolean) {

Boolean new_name = (Boolean) object;

editor.putBoolean(key, new_name);

} else if (object instanceof Integer) {

Integer integer = (Integer) object;

editor.putInt(key, integer);

} else if (object instanceof Float) {

Float f = (Float) object;

editor.putFloat(key, f);

} else if (object instanceof Long) {

Long l = (Long) object;

editor.putLong(key, l);

} else if (object instanceof String) {

String s = (String) object;

editor.putString(key, s);

}

}

flag = editor.commit();

return flag;

}

// 读取数据

public Map getMsg(String fileName) {

Map map = null;

// 读取数据用不到edit

SharedPreferences preferences = getSharedPreferences(fileName,

Context.MODE_APPEND);

//Context.MODE_APPEND能够对已存在的值进行改动

map = preferences.getAll();

return map;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值