android开发接入极光一键登入

释放双眼,带上耳机,听听看~!

为什么要接入极光一键登入?

理由:用户无需输入手机号码,一键点击即可快速完成注册/登录流程,提高转化率优化用户体验。

应用场景

注册 登陆 二次验证

需要费用?

需要

开发文档地址

https://www.jpush.cn/identify

开发步骤:

1.配置环境
登入极光网站,点击设置选择认证设置

在认证设置下,先在集成设置里,填写应用包名和应用签名;然后在一键登入里填入RSA 加密公钥

2.按照极光一键登入文档接入

地址:http://docs.jiguang.cn/jverification/client/android_guide/

2.1 确认 android studio 的 Project 根目录的主 gradle 中配置了 jcenter 支持。(新建 project 默认配置就支持)
buildscript {
repositories {
jcenter()
}

}

allprojects {
repositories {
jcenter()
}
}
2.2在 module 的 gradle 中添加依赖和 AndroidManifest 的替换变量
android {

defaultConfig {
applicationId “com.xxx.xxx” // 您应用的包名.

    ndk {
//选择要添加的对应 cpu 类型的 .so 库。
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
// 还可以添加 'x86', 'x86_64'
}
manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
JPUSH_APPKEY : "你的 Appkey ", //Portal上注册的包名对应的 appKey.
JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
]
......
}
......

}
dependencies {
implementation ‘cn.jiguang.sdk:jverification:2.6.4’
implementation ‘cn.jiguang.sdk:jcore:2.6.0’
}

2.3 看极光Android SDK API开发

2.3.1在application下调用极光一键登入的初始化方法

JVerificationInterface.init(this, 10000, new RequestCallback() {
@Override
public void onResult(int code, String msg) {
if (code == 8000) {
Constant.initSuccess = true;
} else {
Constant.initSuccess = false;
}
}
});

 

2.3.2.写调用一键登入的方法

public static void oneKeyLogin(final Activity activity) {
//判断2秒内是不是连续点击
if (Util.FastClick()) {
Toast.makeText(activity, “请稍后点击”, Toast.LENGTH_LONG).show();
return;
}

 

    try {
//先判断有没有获取读取手机号码状态权限
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (activity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(activity, "请到设置界面获取获取手机号状态权限", Toast.LENGTH_LONG).show();
checkPermissions(Constant.PHONE_STATE, , PermissionsUtil.READ_PHONE_STATE;
}
return;
}
}
//如果一键登入初始化失败,跳转到账号密码登入界面
if (!JVerificationInterface.isInitSuccess() && !Constant.initSuccess) {
LoginActivity.start(activity);
return;
}
//判断之前有没有登人(登入后讲token保存到本地,退出登入和顶号通知时,清空token)
if (TextUtils.isEmpty(LoginHelper.get().getToken())) {
//当前网络环境不支持一键登入,跳转到账号密码登入界面
if (!JVerificationInterface.checkVerifyEnable(activity)) {
LoginActivity.start(activity);
return;
}
//显示正在加载中菊花提示动画
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadProgress();
}


//调起一键登入授权界面
final LoginSettings settings = new LoginSettings();
settings.setAutoFinish(true);//设置登录完成后是否自动关闭授权页
settings.setTimeout(30 * 1000);//设置超时时间,单位毫秒。 合法范围(0,30000],范围以外默认设置为10000
settings.setAuthPageEventListener(new AuthPageEventListener() {
@Override
public void onEvent(int cmd, String msg) {
//do something…
Log.d(“JVerificationInterface”, “[” + cmd + “]message=” + msg);
}
});//设置授权页事件监听

//在预定时间内获取当前在线的sim卡所在运营商及token,如果超过所设时间,接口回调返回超时。如果获取成功代表可以用来验证手机号,获取失败则建议做短信验证。

JVerificationInterface.getToken(activity, 15000, new VerifyListener() {
@Override
public void onResult(int code, String content, String operator) {
//显示了界面,关闭菊花提示弹框
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadDismiss();
}
//2000 获取手机号码和运营商成功
if (code == 2000) {

 




                         //自定义一键登入界面和添加自定义控件
ImageView imBg = new ImageView(activity);
imBg.setImageResource(R.drawable.im_login_bg);
RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(207));
imBg.setScaleType(ImageView.ScaleType.FIT_XY);
imBg.setLayoutParams(mLayoutParams);
ImageView imDel = new ImageView(activity);
imDel.setImageResource(R.drawable.im_login_bg);
RelativeLayout.LayoutParams mLayoutParam = new RelativeLayout.LayoutParams(DisplayUtil.dip2px(24), DisplayUtil.dip2px(24));
imDel.setScaleType(ImageView.ScaleType.FIT_XY);
imDel.setImageResource(R.drawable.im_delete);
imDel.setId(R.id.im_login_delete);
imDel.setPadding(0, DisplayUtil.dip2px(10), DisplayUtil.dip2px(10), 0);
mLayoutParam.setMargins(DisplayUtil.dip2px(15), DisplayUtil.dip2px(10), 0, 0);
imDel.setLayoutParams(mLayoutParam);
TextView tv = new TextView(activity);
tv.setId(R.id.tv_login_info);
tv.setText("欢迎登录XXXAPP");
tv.setTextColor(activity.getResources().getColor(R.color.main_3333));
tv.setTextSize(23);
TextPaint tp = tv.getPaint();
tp.setFakeBoldText(true);
RelativeLayout.LayoutParams mLayoutParamTv = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
mLayoutParamTv.addRule(RelativeLayout.BELOW, R.id.im_login_delete);
mLayoutParamTv.setMargins(DisplayUtil.dip2px(25), DisplayUtil.dip2px(35), 0, 0);
tv.setLayoutParams(mLayoutParamTv);
TextView tv_login_info = new TextView(activity);
tv_login_info.setText("本机号码一键登录");
tv_login_info.setTextColor(activity.getResources().getColor(R.color.main_3333));
tv_login_info.setTextSize(15);
RelativeLayout.LayoutParams mLayoutParamInfo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
mLayoutParamInfo.addRule(RelativeLayout.BELOW, R.id.tv_login_info);
mLayoutParamInfo.setMargins(DisplayUtil.dip2px(25), DisplayUtil.dip2px(35), 0, 0);
tv_login_info.setLayoutParams(mLayoutParamInfo);
TextView tv_login_info_ = new TextView(activity);
tv_login_info_.setText("账号密码登录");
tv_login_info_.setTextColor(activity.getResources().getColor(R.color.main_9999));
tv_login_info_.setTextSize(15);
RelativeLayout.LayoutParams mLayoutParamInfo_ = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
mLayoutParamInfo_.setMargins(0, DisplayUtil.dip2px(398), 0, 0);
mLayoutParamInfo_.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv_login_info_.setLayoutParams(mLayoutParamInfo_);
ImageView im_weixin_login = new ImageView(activity);
im_weixin_login.setId(R.id.im_login_wx);
im_weixin_login.setImageResource(R.drawable.im_login_bg);
RelativeLayout.LayoutParams mLayoutParamWx = new RelativeLayout.LayoutParams(DisplayUtil.dip2px(39), DisplayUtil.dip2px(39));
im_weixin_login.setScaleType(ImageView.ScaleType.FIT_XY);
im_weixin_login.setImageResource(R.drawable.ic_wenxin);
mLayoutParamWx.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLayoutParamWx.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLayoutParamWx.setMargins(0, 0, 0, DisplayUtil.dip2px(108));
im_weixin_login.setLayoutParams(mLayoutParamWx);
TextView tv_wx = new TextView(activity);
tv_wx.setTextColor(activity.getResources().getColor(R.color.main_6666));
tv_wx.setTextSize(14);
tv_wx.setText("微信");
RelativeLayout.LayoutParams mLayoutParamTvWx = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// mLayoutParamTvWx.addRule(RelativeLayout.BELOW, R.id.im_login_wx);
mLayoutParamTvWx.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLayoutParamTvWx.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLayoutParamTvWx.setMargins(0, 0, 0, DisplayUtil.dip2px(79));
tv_wx.setLayoutParams(mLayoutParamTvWx);
TextView tv_agree = new TextView(activity);
tv_agree.setTextColor(activity.getResources().getColor(R.color.main_6666));
tv_agree.setTextSize(12);
tv_agree.setText("登录即同意并阅读");
RelativeLayout.LayoutParams mLayoutParamTvAgree = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// mLayoutParamTvWx.addRule(RelativeLayout.BELOW, R.id.im_login_wx);
mLayoutParamTvAgree.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLayoutParamTvAgree.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLayoutParamTvAgree.setMargins(0, 0, 0, DisplayUtil.dip2px(50));
tv_agree.setLayoutParams(mLayoutParamTvAgree);
SpannableString ss = new SpannableString("  和 《XXX使用协议》");
AbsoluteSizeSpan ass = new AbsoluteSizeSpan(10, true);
ss.setSpan(new ForegroundColorSpan(activity.getResources().getColor(R.color.main_44b8df)), 3, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Button navBtn = new Button(activity);
navBtn.setText("导航栏按钮");
RelativeLayout.LayoutParams navBtnParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
navBtn.setLayoutParams(navBtnParam);
TextView tv_xieyi = new TextView(activity);
tv_xieyi.setTextColor(activity.getResources().getColor(R.color.main_6666));
tv_xieyi.setTextSize(12);
tv_xieyi.setText(ss);
RelativeLayout.LayoutParams mLayoutParamTvXieyi = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// mLayoutParamTvWx.addRule(RelativeLayout.BELOW, R.id.im_login_wx);
mLayoutParamTvXieyi.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mLayoutParamTvXieyi.setMargins(DisplayUtil.dip2px(190), 0, 0, DisplayUtil.dip2px(30));
tv_xieyi.setLayoutParams(mLayoutParamTvXieyi);
JVerifyUIConfig uiConfig = new JVerifyUIConfig.Builder()
.setNavColor(activity.getResources().getColor(R.color.main_transparent_thorough))
.setNavHidden(true)
.setNavText("")
// .setNavReturnImgPath(R.drawable.im_delete + "")
.setNavReturnBtnHeight(14)
.setNavReturnBtnWidth(14)
.setLogoWidth(0)
.setLogoHeight(0)
.setLogoHidden(true)
.setNumFieldOffsetY(252)
.setNumberTextBold(true)
.setSloganHidden(false)
.setPrivacyWithBookTitleMark(true)
.setSloganOffsetY(290)
.setNumberColor(activity.getResources().getColor(R.color.main_3333))
.setLogBtnOffsetY(320)
.setLogBtnText("本机号码一键登录")
.setLogBtnHeight(46)
.setPrivacyState(true)
.setPrivacyCheckboxHidden(true)
.setPrivacyOffsetX(70)
.setAppPrivacyColor(activity.getResources().getColor(R.color.main_44b8df), activity.getResources().getColor(R.color.main_44b8df))
//  .setAppPrivacyOne("应用自定义服务条款一", "https://www.jiguang.cn/about")
//.setAppPrivacyColor(activity.getResources().getColor(R.color.main_6666), )
.setPrivacyText("", "", "", "")
.setStatusBarDarkMode(true)
.setStatusBarColorWithNav(true)
/*.setStatusBarTransparent(true)
.setStatusBarColorWithNav(true)*/
.setNavColor(Color.parseColor("#2244B8DF"))
.setLogBtnImgPath(R.drawable.bg_register_radius_23_all + "")
.setLogBtnTextColor(0xffffffff)
.addCustomView(imBg, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(imDel, true, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
settings.setAutoFinish(true);
}
}).addCustomView(tv, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(tv_login_info, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(tv_login_info_, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
NewLoginActivity.start(activity, true);
}
})
.addCustomView(im_weixin_login, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(tv_wx, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(tv_agree, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).addCustomView(tv_xieyi, false, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
WebActivity.start(activity, Constant.privacyAgreement, "XXX隐私协议");
}
}).addNavControlView(navBtn, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
//  Toast.makeText(context, "导航栏按钮点击", Toast.LENGTH_SHORT).show();
}
}).addCustomView(getTV(activity) == null ? new TextView(activity) : getTV(activity), true, new JVerifyUIClickCallback() {
@Override
public void onClicked(Context context, View view) {
}
}).build();
JVerificationInterface.setCustomUIWithConfig(uiConfig);
//获取一键登入loginToken
JVerificationInterface.loginAuth(activity, settings, new VerifyListener() {
@Override
public void onResult(int code, String content, String operator) {
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadDismiss();
}
//获取到了loginToken,用获取到的loginToken去调后台接口
if (code == 6000) {
//  submit(activity, content);
Log.d("JVerificationInterface", "code=" + code + ", token=" + content + " ,operator=" + operator);
} else if (code == 6001) {
Toast.makeText(activity, "获取登入token失败,请退出重试,或者使用其它方式登入", Toast.LENGTH_SHORT).show();
} else if (code == 6004) {
Toast.makeText(activity, "请退出,稍后再试", Toast.LENGTH_SHORT).show();
}
}
});
} else {
//获取token失败,跳转到账号密码登入界面
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadDismiss();
}
NewLoginActivity.start(activity);
}
}
});
} else {
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadDismiss();
}
//
}
} catch (Exception e) {
Log.d("error", "message=" + e.getLocalizedMessage());
if (null != (BaseActivity) activity) {
((BaseActivity) activity).loadDismiss();
}
}
}

3.运行程序,点击本机号码一键登入,可以在极光后台看登入结果


人已赞赏
Android文章

Android开发Retrofit源码解析

2021-2-1 23:55:01

Android文章

Android开发接入阿里实人认证

2021-2-2 0:37:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索