天天看點

模拟QQ自動登入

模拟QQ自動登入

思想:

  1. 第一步:定義背景賬号和密碼
  2. 第二步:擷取Shared Preference對象

    final SharedPreferences sp = getSharedPreferences(“mrsoft”, MODE_PRIVATE);

    第一個參數指定檔案名,第二個參數指定權限。

  3. 第三步:實作自動登入功能,開始設定的輸入内容為空
  4. 第四步:實作手動登入存儲賬号的功能通過給按鈕添加點選事件,用if語句判斷輸入的密碼是否等于設定的密碼,如果是就跳轉到另外一個界面就是啟動一個activity,否則就列印吐司輸入錯誤。

項目目錄:

模拟QQ自動登入

ativity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
<EditText
    android:id="@+id/username"
    android:hint="qq/郵箱/手機号"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></EditText>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="密碼"></EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="150dp" />

</LinearLayout>
           

activity_message.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".message">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/mes"></ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>
           

java:

MainActivity.java

package com.example.acer.video;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //第一步:定義背景賬号和密碼
    private String mr = "mr";
    private String mrsoft = "mrsoft";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText userNameET = findViewById(R.id.username);  //擷取賬号編輯框
        final EditText userpassET = findViewById(R.id.password);  //擷取密碼編輯框
        Button button = findViewById(R.id.button);        //擷取登入按鈕
        //第二步:擷取Shared Preference對象
        final SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
        //第一個參數指定檔案名,第二個指定權限
        /*********************第三步:實作自動登入功能***********************************/
        String username = sp.getString("username", null);  //擷取賬号資訊
        String password = sp.getString("password", null);  //擷取密碼
        if (username != null && password != null) {    //判斷賬号密碼是否為空
            if (username.equals(mr) && password.equals(mrsoft)) {
                Intent intent = new Intent(MainActivity.this, message.class);
                startActivity(intent);
            }

        } else {
            /************************第四步:實作手動登入存儲賬号的功能**************************/
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String in_userName = userNameET.getText().toString(); //擷取輸入的賬号
                    String in_password = userpassET.getText().toString();//擷取輸入的密碼
                    SharedPreferences.Editor editor = sp.edit();  //擷取editer對象
                    if (in_userName.equals(mr) && in_password.equals(mrsoft)) {
                        editor.putString("username", in_userName);   //儲存賬号
                        editor.putString("password", in_password);   //儲存密碼
                        editor.commit();    //送出資訊
                        Intent intent = new Intent(MainActivity.this, message.class);
                        startActivity(intent);  //啟動跳轉頁面
                        Toast.makeText(MainActivity.this, "已儲存賬号和密碼", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "賬号和密碼錯誤", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }
}

           

message.java

package com.example.acer.video;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class message extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
    }
}

           

顯示結果:

當輸入賬号密碼的時候,就會跳轉到另外一個界面,退出之後再進入就會自動進入界面。

模拟QQ自動登入
模拟QQ自動登入