Android进阶之路

来源:互联网 发布:大数据建设 一流公司 编辑:程序博客网 时间:2024/05/19 18:13

在Android中关于登陆的拦截的问题,只要你的项目不是做的企业内部项目的话,都会用到这个功能!在电商平台,或者P2P的一些平台,大多有存在一个“个人模块”,其中的关于我们的介绍模块是无需用户登录的,只有涉及到账户信息的时候,才会去拦截用户进行登陆。

登陆拦截的实现方式有三种
1.第一种使用单例模式进行存储登陆状态,较为好用(成熟使用者)
2.第二种使用sp进行存储,简单方便(适合新手)
3.第三种创建一个拦截器进行拦截(网上很多关于这种的,但是在我目前看来狗屁不是,反正我是没用过)

Effect :

这里写图片描述

以下知识,静心慢慢看!

实现效果:
模拟用户操作,一个是无拦截界面,一个为拦截用户的界面,关于拦截界面,需要用户登陆的时候才可查看,无拦截的界面无需用户登陆

思维方式:
首先最主要的我们需要一个存储容器,用于存储用户当前的状态,以此来判断是处于登陆状态还是处于未登陆状态!
其次当用户点击执行相关操作的时候,判断容器内的状态决定是不是拦截用户,一般状态为false ,只有在登陆的时候我们设置对应的状态为true!

前期提要:

Activity 注册:

    <activity android:name=".LoginActivity"/>        <activity android:name=".NoneActivity"/>        <activity android:name=".OwnActivity"/>

Session(最重要的类,也就是容器类) :

package com.yl.intercept.loginintercept;/** * Created by YongLiu on 2017/7/27. */public class Session {    public String token;    public Boolean state;    private static Session session=null;    public static Session getInstence(){        if(session==null){            session=new Session();        }        return session;    }    public String getToken() {        return token;    }    public void setToken(String token) {        this.token = token;    }    public Boolean getState() {        return state;    }    public void setState(Boolean state) {        this.state = state;    }}

MainActivity (在此类我们主要进行了用户的状态判断):

package com.yl.intercept.loginintercept;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private TextView mNone;    private TextView mOwn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mNone = (TextView) findViewById(R.id.btn_intercept_none);        mOwn = (TextView) findViewById(R.id.btn_intercept_own);        mNone.setOnClickListener(this);        mOwn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_intercept_none:                startActivity(new Intent(MainActivity.this, NoneActivity.class));                break;            case R.id.btn_intercept_own:                Boolean state = Session.getInstence().getState();                if (state != null) {                    if (state == true) {                        startActivity(new Intent(MainActivity.this, OwnActivity.class));                    } else {                        Toast.makeText(MainActivity.this, "内部被拦截中,跳转登陆,请设置为true!", Toast.LENGTH_SHORT).show();                    }                }else{                    Toast.makeText(MainActivity.this, "外部被拦截中,跳转登陆", Toast.LENGTH_SHORT).show();                    startActivity(new Intent(MainActivity.this,LoginActivity.class));                }                break;        }    }}

LoginActivity(正常登陆会请求服务器,这里我们模拟的数据。账户:520 密码:1314) :

package com.yl.intercept.loginintercept;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;/** * Created by YongLiu on 2017/7/27. */public class LoginActivity extends AppCompatActivity {    private EditText mAccount;    private EditText mPassword;    private TextView mLogin;    private String dPassword;    private String dAccount;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        initView();        mLogin.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                dAccount = mAccount.getText().toString().trim();                dPassword = mPassword.getText().toString().trim();                Log.e("TAG","dAccount = " + dAccount + "dPassword = " + dPassword);                if(TextUtils.isEmpty(dAccount)||TextUtils.isEmpty(dPassword)){                    Toast.makeText(LoginActivity.this,"账号或者密码为空",Toast.LENGTH_SHORT).show();                }else if(!dAccount.equals("520")||!dPassword.equals("1314")){                    Toast.makeText(LoginActivity.this,"账号或者密码错误",Toast.LENGTH_SHORT).show();                }else if(dAccount.equals("520")&&dPassword.equals("1314")){                    Session.getInstence().setToken("5201314");                    Session.getInstence().setState(true);                    startActivity(new Intent(LoginActivity.this,MainActivity.class));                    finish();                }            }        });    }    private void initView() {        mAccount = (EditText)  findViewById(R.id.et_account);        mPassword = (EditText) findViewById(R.id.et_password);        mLogin = (TextView) findViewById(R.id.btn_login);    }}

MainActivity Xml :

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.yl.intercept.loginintercept.MainActivity">    <LinearLayout        android:layout_centerInParent="true"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >    <TextView        android:id="@+id/btn_intercept_none"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="5dp"        android:gravity="center"        android:text="无拦截状态" />    <TextView        android:layout_marginTop="10dp"        android:id="@+id/btn_intercept_own"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:padding="5dp"        android:gravity="center"        android:text="有拦截状态" />    </LinearLayout></RelativeLayout>

LoginActivity Xml :

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <LinearLayout        android:layout_centerInParent="true"        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_height="wrap_content">    <LinearLayout        android:layout_width="match_parent"        android:orientation="horizontal"        android:layout_height="wrap_content">        <TextView            android:layout_marginLeft="5dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="14sp"            android:text="账号:"            />        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/et_account"            />    </LinearLayout>    <LinearLayout        android:layout_marginTop="5dp"        android:layout_width="match_parent"        android:orientation="horizontal"        android:layout_height="wrap_content">        <TextView            android:layout_marginLeft="5dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="14sp"            android:text="密码:"            />        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/et_password"            />    </LinearLayout>    <TextView        android:layout_marginTop="8dp"        android:id="@+id/btn_login"        android:layout_width="80dp"        android:layout_height="wrap_content"        android:padding="10dp"        android:gravity="center"        android:text="登陆"        android:layout_gravity="center"        android:background="#21aae3"        android:textColor="#fff"        />    </LinearLayout></RelativeLayout>

以下代码只是一个UI承载界面,并不是很重要!

NoneActivity :

package com.yl.intercept.loginintercept;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;/** * Created by YongLiu on 2017/7/27. */public class NoneActivity extends AppCompatActivity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_none);        TextView mBack = (TextView) findViewById(R.id.back_none);        mBack.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                finish();            }        });    }}

NoneActivity Xml :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <TextView        android:id="@+id/back_none"        android:layout_width="match_parent"        android:layout_height="40dp"        android:text="返回"        android:gravity="center"        android:background="#21aae3"        android:textColor="#fff"        />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="Baby!此页面是无拦截界面,一般展示的是介绍内容"        android:gravity="center"        android:textColor="#468"        /></LinearLayout>

OwnActivity :

package com.yl.intercept.loginintercept;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.TextView;/** * Created by YongLiu on 2017/7/27. */public class OwnActivity extends AppCompatActivity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_own);        TextView mBack = (TextView) findViewById(R.id.back_own);        mBack.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                finish();            }        });    }}

OwnActivity Xml :

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="vertical"    android:layout_height="match_parent">    <TextView        android:id="@+id/back_own"        android:layout_width="match_parent"        android:layout_height="40dp"        android:text="返回"        android:gravity="center"        android:background="#21aae3"        android:textColor="#fff"        />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="Baby!此页面是被拦截界面,一般有Token才能进入,因为需要获取用户的私人信息,如电商平台和P2P平台下的订单之类"        android:gravity="center"        android:textColor="#468"        /></LinearLayout>

如果你想学习SP的方式实现登陆拦截,请留言,这样我会后续补入!

原创粉丝点击