详解Android的登录那点事

发布时间 - 2026-01-10 21:57:44    点击率:

随着互联网的高速发展,一个应用为了保护用户的隐私,通常会通过设置用户名+密码的验证方式保证用户隐私的相对安全,我知道一般网站的登录验证,通常会设置一个二维码,通过验证二维码,防止恶意软件通过机械程序,对用户密码进行破解,那么Android设备如何实现这个功能呢?相信很多开发者对此不屑一顾,因为这样增加了用户使用的复杂性,很多软件是不会这样设计的,现在我们暂且不谈它是不是有用,今天我们重点探讨一下,如何在Android的设备上实现这个功能。本篇为大家介绍的内容包括:1、用户连续多次输错密码,增加验证码验证;2、Android如何通过http请求达到与服务器之间的通讯。好了下面开始我们今天内容的介绍,首先我们先一起来学习一下如何实现用户连续多次输错密码,增加验证码功能。

既然用的到二维码,那么Android如何生成二维码呢?为大家提供一个生成二维码的类:

//生成二维码的类
public class BPUtil {
 /**
  * 用于生成二维码的字符
  */
 private static final char[] CHARS = {
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  'a','b','c','d','e','f','g','h','i','j','k','m','l','n','o','p','q','r','s','t','u','v','w','x','y','z',
  'A','B','C','D','E','F','G','H','I','J','K','M','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
 };
 private static BPUtil bpUtil;
 public static BPUtil getInstance() {
  if(bpUtil == null)
   bpUtil = new BPUtil();
  return bpUtil;
 }
//   width="60" height="30" 
//    base_padding_left="5"
//   range_padding_left="10"
//   base_padding_top="15"
//   range_padding_top="10"
//   codeLength="4"
//   line_number="3"
//   font_size="20"
 //default settings
 private static final int DEFAULT_CODE_LENGTH = 4;
 private static final int DEFAULT_FONT_SIZE = 20;
 private static final int DEFAULT_LINE_NUMBER = 3;
 private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10;
 private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30;
 //settings decided by the layout xml
 //canvas width and height
 private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; 
 //random word space and pading_top
 private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT, 
   base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
 //number of chars, lines; font size
 private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
 //variables
 private String code;
 private int padding_left, padding_top;
 private Random random = new Random();
 public Bitmap createBitmap() {
  padding_left = 0;
  Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
  Canvas c = new Canvas(bp);
  code = createCode();
  c.drawColor(Color.WHITE);
  Paint paint = new Paint();
  paint.setTextSize(font_size);
  for (int i = 0; i < code.length(); i++) {
   randomTextStyle(paint);
   randomPadding();
   c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
  }
  for (int i = 0; i < line_number; i++) {
   drawLine(c, paint);
  }
  c.save( Canvas.ALL_SAVE_FLAG );//保存 
  c.restore();//
  return bp;
 }
 public String getCode() {
   return bpUtil.createCode();
 }
 private String createCode() {
  StringBuilder buffer = new StringBuilder();
  for (int i = 0; i < codeLength; i++) {
   buffer.append(CHARS[random.nextInt(CHARS.length)]);
  }
  return buffer.toString();
 }
 private void drawLine(Canvas canvas, Paint paint) {
  int color = randomColor();
  int startX = random.nextInt(width);
  int startY = random.nextInt(height);
  int stopX = random.nextInt(width);
  int stopY = random.nextInt(height);
  paint.setStrokeWidth(1);
  paint.setColor(color);
  canvas.drawLine(startX, startY, stopX, stopY, paint);
 }
 private int randomColor() {
  return randomColor(1);
 }
 private int randomColor(int rate) {
  int red = random.nextInt(256) / rate;
  int green = random.nextInt(256) / rate;
  int blue = random.nextInt(256) / rate;
  return Color.rgb(red, green, blue);
 }
 private void randomTextStyle(Paint paint) {
  int color = randomColor();
  paint.setColor(color);
  paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
  float skewX = random.nextInt(11) / 10;
  skewX = random.nextBoolean() ? skewX : -skewX;
  paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
//  paint.setUnderlineText(true); //true为下划线,false为非下划
//  paint.setStrikeThruText(true); //true为删除线,false为非删除
 }
 private void randomPadding() {
  padding_left += base_padding_left + random.nextInt(range_padding_left);
  padding_top = base_padding_top + random.nextInt(range_padding_top);
 }
}

有了二维码,下面我们开始设计我们的功能,这里先简单说一下,我们最终要实现的功能:1、用户正常输入用户名+密码登录;2、当用户连续3次输错密码,要求用户之后必须增加验证码输入验证。下面我们开始功能设计实现,首先是我们的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="${relativePackage}.${activityClass}" 
 android:orientation="vertical"
 >
 <LinearLayout 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:text="@string/yanzheng" />
 <LinearLayout 
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
   <TextView
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/name" />
   <EditText 
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_weight="6"
    android:layout_height="wrap_content"
    android:hint="@string/name_new"
    android:singleLine="true"
    />
  </LinearLayout>
  <LinearLayout 
  android:orientation="horizontal"
   android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
   <TextView
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/password" />
   <EditText 
    android:id="@+id/pass"
    android:layout_weight="6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/pass_new"
    android:singleLine="true"
    />
  </LinearLayout>
  <LinearLayout 
  android:id="@+id/layout_yanzhengma"
  android:visibility="gone"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
   <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/yanzhengma" />
   <TextView
    android:id="@+id/rander"
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
   <EditText
    android:id="@+id/rander_input"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    />
  </LinearLayout>
  <LinearLayout 
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
   <Button
   android:id="@+id/get"
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/get" />
   <Button
     android:id="@+id/post"
   android:layout_weight="1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/post" />
  </LinearLayout>
  </LinearLayout>
</RelativeLayout>

重点的内容来了,我们主Activity代码,大家先看一下吧:

public class MainActivity extends Activity {
 private TextView mytext = null;
 private EditText myname = null;//用户名
 private EditText mypass = null;//密码
 private EditText myrander = null;//验证码
 private Button mygetbutton = null;//Get方式发送Http请求
 private Button mypostbutton = null;//Post方式发送Http请求
 private LinearLayout myline = null;//控制二维码的显示
 private static int n = 0;//用户输错密码次数统计
 static String name = null;//用户输入的用户名
 static String password = null;//用户输入的密码
 private String edit;//用户输入用户输入的验证码
 private String code;//验证码
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mytext = (TextView)findViewById(R.id.rander);
  myname = (EditText)findViewById(R.id.name);
  mypass = (EditText)findViewById(R.id.pass);
  myrander = (EditText)findViewById(R.id.rander_input);
  mygetbutton = (Button)findViewById(R.id.get);
  mypostbutton = (Button)findViewById(R.id.post);
  myline = (LinearLayout)findViewById(R.id.layout_yanzhengma);
  mygetbutton.setOnClickListener(new mygetbutton());
  mypostbutton.setOnClickListener(new mypostbutton());

 } 
 class mygetbutton implements OnClickListener{
  public void onClick(View v) {
   name = myname.getText().toString();
   password = mypass.getText().toString();
   if(n>=3){//连续三次输错密码
    edit = myrander.getText().toString();
    boolean boo = captcha (code , edit);
    if(boo){
     new Thread(new Runnable() {
      public void run() {
       final boolean flag = SendServer.getsave(name, password);
       runOnUiThread(new Runnable() {
        public void run() {
         if(flag){
  Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
          n=0;
         }else{
 Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
          n++;
          if(n>=3){
  myline.setVisibility(View.VISIBLE);
          }
         }
        }
       });
      }
     }).start();
    }else{
     code = BPUtil.getInstance().getCode().toLowerCase();//生成新的二维码
     mytext.setText(code);//展示在用户面前
    }
   }else{
    new Thread(new Runnable() {
     public void run() {
      final boolean flag = SendServer.getsave(name, password);
      runOnUiThread(new Runnable() {
       public void run() {
        if(flag){
         Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
         n=0;
        }else{
         Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
         n++;
         if(n>=3){
 myline.setVisibility(1);
          code = BPUtil.getInstance().getCode().toLowerCase();
     mytext.setText(code);
         }
        }
       }
      });
     }
    }).start();
   }
  }
 }
 class mypostbutton implements OnClickListener{
  public void onClick(View v) {
   name = myname.getText().toString();
   password = mypass.getText().toString();
   new Thread(new Runnable() {
    public void run() {
     final boolean flag = SendServer.postsave(name, password);
     runOnUiThread(new Runnable() {
      @Override
      public void run() {
       if(flag){
        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        n=0;
       }else{
        Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        n++;
        if(n>=3){
    myline.setVisibility(1);
        }
       }
      }
     });
    }
   }).start();
  }
 }
 @SuppressLint("ShowToast")
 private boolean captcha (String code , String edit) {
  boolean flag = false; 
  if (code.equals(edit)) {
   flag = true;
  }else {
   flag = false;
   Toast.makeText(MainActivity.this, " 验证码错误", 0).show();
//   imageview.setImageBitmap(bitmap);
  }
  return flag;
 }
 protected void onRestart() {
  super.onRestart();
  n=0;
 }
 protected void onDestroy() {
  super.onDestroy();
  n=0;
 }
}

在这里简单介绍一下代码,因为本篇接下来要为大家分享关于Android发送GET、POST请求的知识,这里我写了两个按钮,一个用来通过使用GET方式验证,一个通过使用POST方式验证,功能上是一致的。最后请大家注意一下:红色字体部分,红色字体部分就是我们通过调用上部二维码生成类,生成二维码,然后展示在用户界面。还有就是:onRestart、onDestroy都是Activity的生命周期函数,这里将n归零,方便我们的再次登录体验。好了到这里我们的第一部分就完成了,下面我们开始进入我们本篇的下半部分。

关于Android服务器请求,一般有两种方式:GET、POST两种方式,接下来我们就开始一起学习吧。

public class SendServer {
 //GET方式请求
 public static boolean getsave(String name, String password) {
  boolean flag = false;
  HttpURLConnection conn = null;
  try {
   //防止中文乱码
   String username = URLEncoder.encode(name, "UTF-8");
   String userpassword = URLEncoder.encode(password, "UTF-8");
   URL url = new URL("http://10.20.90.3:8080/Register/ManageServlet?"+"name="+username+"&password="+userpassword);
   conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("GET");
   conn.setConnectTimeout(5000);
   conn.setReadTimeout(5000);
   int responseCode = conn.getResponseCode();
   if(responseCode == 200){
    InputStream is = conn.getInputStream();
    String stu = getStringFromInputStream(is);
    if(stu.equals("登录成功")){
     flag = true;
    }
   }
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  } finally{
   if(conn != null){
    conn.disconnect(); //关闭连接
   }
  }
  return flag;
 }
 //POST请求
 public static boolean postsave(String name, String password) {
  boolean flag = false;
  HttpURLConnection conn = null;
  try {
   URL url = new URL("http://10.20.90.3:8080/Register/ManageServlet?");
   conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("POST");
   conn.setConnectTimeout(5000);
   conn.setReadTimeout(5000);
   //post请求参数
   String data = "name="+name+"&password="+password;
   OutputStream out = conn.getOutputStream();
   out.write(data.getBytes());
   out.flush();
   out.close();
//   conn.setRequestProperty("Content-Length", 500);// 内容长度设置为500;请求头消息格式设置
   int respon = conn.getResponseCode();
   if(respon == 200){
    InputStream is = conn.getInputStream();
    String stu = getStringFromInputStream(is);
    if(stu.equals("登录成功")){
     flag = true;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(conn != null){
    conn.disconnect();
   }
  }
  return flag;
 }
 //解析服务返回的请求
 private static String getStringFromInputStream(InputStream is) throws Exception{
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024*8];
  int len = -1;
  while((len = is.read(buffer)) != -1){
   baos.write(buffer, 0, len);
  }
  is.close();
  String html = new String(baos.toByteArray(), "GBK");//接收服务器端的数据时,防止出现中文乱码。

  //String html = baos.toString();
  baos.close();
  return html;
 }
}

好了结束了,内容简单,大家自行了解吧。新手学习,高手交流。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


# Android  # 登录  # Android实现使用微信登录第三方APP的方法  # Android端实现单点登录的方法详解  # Android 第三方登录、分享(ShareSDK、友盟)  # Android集成微信登录的步骤详解  # Android QQ登录界面绘制代码  # Android开发实例之登录界面的实现  # Android属性动画实现炫酷的登录界面  # Android调用第三方QQ登录代码分享  # Android设计登录界面、找回密码、注册功能  # Android集成新浪微博第三方登录的方法  # Android开发之登录验证实例教程  # 验证码  # 好了  # 通常会  # 如何实现  # 粗体  # 都是  # 互联网  # 来了  # 在这里  # 周期函数  # 两种  # 下划线  # 请大家  # 它是  # 写了  # 有两种  # 提供一个  # 要为  # 设置为  # 输入用户名 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel中DTO是什么概念_在Laravel项目中使用数据传输对象(DTO)  如何基于云服务器快速搭建个人网站?  什么是JavaScript解构赋值_解构赋值有哪些实用技巧  在线教育网站制作平台,山西立德教育官网?  实例解析Array和String方法  Laravel如何使用Guzzle调用外部接口_Laravel发起HTTP请求与JSON数据解析【详解】  奇安信“盘古石”团队突破 iOS 26.1 提权  Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】  Laravel如何生成URL和重定向?(路由助手函数)  Laravel如何使用Contracts(契约)进行编程_Laravel契约接口与依赖反转  高防服务器租用指南:配置选择与快速部署攻略  如何快速辨别茅台真假?关键步骤解析  Laravel怎么实现微信登录_Laravel Socialite第三方登录集成  如何快速上传建站程序避免常见错误?  MySQL查询结果复制到新表的方法(更新、插入)  悟空识字如何进行跟读录音_悟空识字开启麦克风权限与录音  Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置  UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】  高防服务器:AI智能防御DDoS攻击与数据安全保障  PHP 500报错的快速解决方法  移动端脚本框架Hammer.js  如何将凡科建站内容保存为本地文件?  如何在七牛云存储上搭建网站并设置自定义域名?  Laravel如何为API生成Swagger或OpenAPI文档  Python自动化办公教程_ExcelWordPDF批量处理案例  个人网站制作流程图片大全,个人网站如何注销?  Laravel Fortify是什么,和Jetstream有什么关系  深圳网站制作的公司有哪些,dido官方网站?  深圳网站制作平台,深圳市做网站好的公司有哪些?  创业网站制作流程,创业网站可靠吗?  php485函数参数是什么意思_php485各参数详细说明【介绍】  如何在阿里云购买域名并搭建网站?  香港服务器建站指南:外贸独立站搭建与跨境电商配置流程  高防网站服务器:DDoS防御与BGP线路的AI智能防护方案  如何在云服务器上快速搭建个人网站?  小米17系列还有一款新机?主打6.9英寸大直屏和旗舰级影像  Laravel如何配置Horizon来管理队列?(安装和使用)  高防服务器如何保障网站安全无虞?  如何快速生成高效建站系统源代码?  Laravel怎么连接多个数据库_Laravel多数据库连接配置  大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?  网站建设要注意的标准 促进网站用户好感度!  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  WordPress 子目录安装中正确处理脚本路径的完整指南  Laravel如何部署到服务器_线上部署Laravel项目的完整流程与步骤  Internet Explorer官网直接进入 IE浏览器在线体验版网址  Firefox Developer Edition开发者版本入口  微信小程序 input输入框控件详解及实例(多种示例)  iOS UIView常见属性方法小结  今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】