关于sdk:MobTech秒验-Android端如何在授权界面添加短信登录按钮

31次阅读

共计 10409 个字符,预计需要花费 27 分钟才能阅读完成。

SecVerifySDK 提供了两种自定义受权界面的形式,别离为 UISetting,举荐应用 Adapter 形式来进行受权界面的自定义,也可应用 Adapter 联合 UISetting 的形式(如实现关上页面动画)。

编写 xml 布局

应用 adapter 通过自行编写受权页面布局的形式来实现自定义,所以须要先绘制本人的 xml 布局。在 res/layout 下新建 sec_diy.xml,如图:

编写 xml 内容,可参考下方代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="#FFF4EB"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:adjustViewBounds="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:textSize="18sp"
        android:textStyle="bold"
        tools:text="135****5851" />

    <Button
        android:id="@+id/bt_login"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/loginbutton_bg"
        android:text="登录"
        android:textColor="#fff" />

    <TextView
        android:id="@+id/tv_agreement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:text="登录即批准中国移不动认证服务协定并应用本机号码登录" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="中国移不动提供认证服务" />

    <ImageView
        android:id="@+id/iv_sms_login"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/icon_sms" />
</LinearLayout>

编写代码

在我的项目中任意地位创立 MyLoginAdapter 类,编写代码如下:

package xx.xx.xxx;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mob.MobSDK;
import com.mob.secverify.SecVerify;
import com.mob.secverify.ui.AgreementPage;
import com.mob.secverify.ui.component.LoginAdapter;
import com.mz.smssdk.SMSActivity;

/**
* 应用 Adapter 的形式批改受权页面 ui, 通过批改受权页面的控件属性,达到批改目标
* <p>
* * todo 须要留神以下内容
* *  1、在完结以后受权页面时须要调用 SecVerify.finishOAuthPage(); 来完结,否则会影响下次进入
* *  2、在点击登录之后,不管登录胜利或者失败,须要 SecVerify.finishOAuthPage(); 完结以后页面,否则会影响电信受权页面的回调导致页面无奈完结
* *  3、设置脱敏手机号须要在 onResume 中设置,onCreate 中可能还未拿到这个脱敏手机号
*/
public class MyLoginAdapter extends LoginAdapter {
    private Activity activity;
    private ViewGroup vgBody;
    private LinearLayout vgContainer;
    private RelativeLayout rlTitle;
    private Button btnLogin;
    private TextView tvSecurityPhone;
    private TextView tvOwnPhone;
    private TextView tvAgreement;
    private CheckBox cbAgreement;
    private View contentView;
    // 可用于判断展现运营商隐衷协定
    private String operator;
    private String url;

    @Override
    public void onCreate() {super.onCreate();
        // 获取受权页面原有控件
        init();
        // 沉迷状态栏
        setImmTheme();}

    private void init() {vgBody = getBodyView();
        vgContainer = (LinearLayout) getContainerView();
        activity = getActivity();
        rlTitle = getTitlelayout();
        btnLogin = getLoginBtn();
        tvSecurityPhone = getSecurityPhoneText();

        cbAgreement = getAgreementCheckbox();
        operator = getOperatorName();
        // 暗藏 受权页面原有内容
        vgBody.setVisibility(View.GONE);
        rlTitle.setVisibility(View.GONE);
        // 获取本人的 View
        contentView = View.inflate(activity, R.layout.sec_diy, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        // 增加本人的 View 到受权页面上,留神不要应用 Activity 来设置
        vgContainer.addView(contentView, params);
    }

    @Override
    public void onResume() {super.onResume();
        //        弹出脱敏手机号
        Toast.makeText(activity, "脱敏手机号" + tvSecurityPhone.getText(), Toast.LENGTH_SHORT).show();
        //        获取本人 xml 的手机号 TextView
        TextView phone = contentView.findViewById(R.id.tv_phone);
        //        给本人 xml 的手机号控件赋值
        phone.setText(getSecurityPhoneText().getText());
        //        给本人 xml 的手机号控件增加点击事件
        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {SecVerify.finishOAuthPage();
                Toast.makeText(activity, "脱敏手机号" + tvSecurityPhone.getText(), Toast.LENGTH_SHORT).show();}
        });

        //        获取本人 xml 的登录按钮 Button
        Button button = contentView.findViewById(R.id.bt_login);
        ImageView smsLogin = contentView.findViewById(R.id.iv_sms_login);
        //        给本人 xml 的登录按钮 Button 增加点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//                Toast.makeText(activity, "cbAgreement.isChecked():" + cbAgreement.isChecked(), Toast.LENGTH_SHORT).show();
                // 肯定要解决!!!!点击本人登录按钮时须要将默认的复选框设置为选中,并且点击原有的受权页面登录按钮
                // 肯定要解决!!!!点击本人登录按钮时须要将默认的复选框设置为选中,并且点击原有的受权页面登录按钮
                // 肯定要解决!!!!点击本人登录按钮时须要将默认的复选框设置为选中,并且点击原有的受权页面登录按钮
                cbAgreement.setChecked(true);
                btnLogin.performClick();}
        });
        smsLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {activity.startActivity(new Intent(activity, SMSActivity.class));
            }
        });
        //        TextView agreement = contentView.findViewById(R.id.agreement);
        //        agreement.setText(buildSpanString());
    }

    private SpannableString buildSpanString() {
        String operatorText = "";
        if (OperatorUtils.getCellularOperatorType() == 1) {
            operatorText = "《中国移动认证服务条款》";
            url = "https://wap.cmpassport.com/resources/html/contract.html";
        } else if (OperatorUtils.getCellularOperatorType() == 2) {
            operatorText = "《中国联通认证服务条款》";
            url = "https://ms.zzx9.cn/html/oauth/protocol2.html";
        } else if (OperatorUtils.getCellularOperatorType() == 3) {
            operatorText = "《中国电信认证服务条款》";
            url = "https://e.189.cn/sdk/agreement/content.do?type=main&appKey=&hidetop=true&returnUrl=";
        }
        String ageementText = "登录即批准" + operatorText + "及《自有隐衷协定》和" +
            "《自有服务策略》、《其余隐衷协定》并受权秒验应用本机号码登录";
        String cusPrivacy1 = "《自有隐衷协定》";
        String cusPrivacy2 = "《自有服务策略》";
        String cusPrivacy3 = "《其余隐衷协定》";
        int baseColor = MobSDK.getContext().getResources().getColor(R.color.balck);
        int privacyColor = Color.parseColor("#FFFE7A4E");
        int cusPrivacyColor1 = Color.parseColor("#FF4E96FF");
        int cusPrivacyColor2 = Color.parseColor("#FF4E96FF");
        int cusPrivacyColor3 = Color.parseColor("#FFFE7A4E");
        SpannableString spanStr = new SpannableString(ageementText);
        int privacyIndex = ageementText.indexOf(operatorText);
        spanStr.setSpan(new ForegroundColorSpan(baseColor)
                        , 0, ageementText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置文字的单击事件
        spanStr.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {ds.setUnderlineText(false);
            }

            @Override
            public void onClick(View widget) {gotoAgreementPage(url, "");
            }
        }, privacyIndex, privacyIndex + operatorText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置文字的前景色
        spanStr.setSpan(new ForegroundColorSpan(privacyColor), privacyIndex, privacyIndex + operatorText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (!TextUtils.isEmpty(cusPrivacy1)) {int privacy1Index = ageementText.indexOf(cusPrivacy1);
            // 设置文字的单击事件
            spanStr.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(TextPaint ds) {ds.setUnderlineText(false);
                }

                @Override
                public void onClick(View widget) {gotoAgreementPage("https://www.mob.com", null);
                    //                    if (wrapper != null && wrapper.cusAgreement1Clicked != null){//                        wrapper.cusAgreement1Clicked.handle();
                    //                    }
                }
            }, privacy1Index, privacy1Index + "《自有隐衷协定》".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 设置文字的前景色
            spanStr.setSpan(new ForegroundColorSpan(cusPrivacyColor1), privacy1Index, privacy1Index + cusPrivacy1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (!TextUtils.isEmpty(cusPrivacy2)) {int privacy2Index = ageementText.lastIndexOf(cusPrivacy2);
            // 设置文字的单击事件
            spanStr.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(TextPaint ds) {ds.setUnderlineText(false);
                }

                @Override
                public void onClick(View widget) {gotoAgreementPage("https://www.baidu.com", null);
                }
            }, privacy2Index, privacy2Index + cusPrivacy2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 设置文字的前景色
            spanStr.setSpan(new ForegroundColorSpan(cusPrivacyColor2), privacy2Index, privacy2Index + cusPrivacy2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        if (!TextUtils.isEmpty(cusPrivacy3)) {int privacy3Index = ageementText.lastIndexOf(cusPrivacy3);
            // 设置文字的单击事件
            spanStr.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(TextPaint ds) {ds.setUnderlineText(false);
                    ds.linkColor = Color.parseColor("#FFFFFF");
                }

                @Override
                public void onClick(View widget) {gotoAgreementPage("https://www.baidu.com", null);
                }
            }, privacy3Index, privacy3Index + cusPrivacy3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            // 设置文字的前景色
            spanStr.setSpan(new ForegroundColorSpan(cusPrivacyColor3), privacy3Index, privacy3Index + cusPrivacy3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spanStr;
    }

    // 可替换为跳转本人的 webview
    private static void gotoAgreementPage(String agreementUrl, String title) {if (TextUtils.isEmpty(agreementUrl)) {return;}
        AgreementPage page = new AgreementPage();
        Intent i = new Intent();
        i.putExtra("extra_agreement_url", agreementUrl);
        if (!TextUtils.isEmpty(title)) {i.putExtra("privacy", title);
        }
        page.show(MobSDK.getContext(), i);
    }

    private void setImmTheme() {if (Build.VERSION.SDK_INT >= 21) {
            // 设置沉迷式状态栏
            View decorView = activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //             设置状态栏通明
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            if (Build.VERSION.SDK_INT >= 23) {activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
        // 是否占用状态栏的地位,false 为占用,true 为不占用
        vgContainer.setFitsSystemWindows(false);
        // 是否全屏
        //        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 如果不想适配 P 以上的水滴屏和刘海屏,能够在这里设置 layoutInDisplayCutoutMode 为其余的值
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            activity.getWindow().setAttributes(lp);
        }
    }


    public MyLoginAdapter() {}

    @Override
    public void onDestroy() {super.onDestroy();
        Toast.makeText(activity, "我敞开了", Toast.LENGTH_SHORT).show();}
}

domo 体验

编写一键登录代码

SecVerify.setAdapterClass(MyLoginAdapter.class);
SecVerify.verify(null,null);

至此,您已实现了在受权页面增加短信登录按钮,欢快的游玩吧。

正文完
 0