Android如何集成极光短信验证
程序员文章站
2024-02-28 17:06:04
先借助android studio工具新建一个新的空项目。
步骤一(获取appkey)
1.在极光官网平台上新建短信应用(根据要求包名和应用名称填写好)
2....
先借助android studio工具新建一个新的空项目。
步骤一(获取appkey)
1.在极光官网平台上新建短信应用(根据要求包名和应用名称填写好)
2.获取得到jpush_appkey
步骤二 (导入第三方库和so文件)
1.用android studio打开应用
2.切换应用状态android为project状态
3.在libs文件夹下把对应的jar文件拷入 然后as libraries
步骤三(初始化以及调用)
1.新建application初始化
public class myapp extends application { @override public void oncreate() { super.oncreate(); smssdk.getinstance().setdebugmode(true); smssdk.getinstance().initsdk(this); } }
2.在activity中调用
public class mainactivity extends baseactivity { edittext edit_name; edittext edit_pwd; edittext sms; string name; string pwd; button get_sms; button btn_login; private handler handler = new handler() { @override public void handlemessage(message msg) { super.handlemessage(msg); switch (msg.what) { case 1: toast.maketext(mainactivity.this, msg.obj.tostring(), toast.length_short).show(); break; case 2: toast.maketext(mainactivity.this, msg.obj.tostring(), toast.length_short).show(); break; case 3: toast.maketext(mainactivity.this, msg.obj.tostring(), toast.length_short).show(); break; case 4: toast.maketext(mainactivity.this, msg.obj.tostring(), toast.length_short).show(); break; } } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); init(); } private void init() { edit_name = (edittext) findviewbyid(r.id.edit_name); edit_pwd = (edittext) findviewbyid(r.id.edit_pwd); sms = (edittext) findviewbyid(r.id.sms); get_sms = (button) findviewbyid(r.id.get_sms); btn_login = (button) findviewbyid(r.id.btn_login); get_sms.setonclicklistener(new getsmsclicklistener()); btn_login.setonclicklistener(new loginclicklistener()); } public class getsmsclicklistener implements view.onclicklistener { @override public void onclick(view view) { name = edit_name.gettext().tostring().trim(); pwd = edit_pwd.gettext().tostring().trim(); if (name.equals("")) { edit_name.requestfocus(); } else if (pwd.equals("")) { edit_pwd.requestfocus(); } else { new thread(new runnable() { @override public void run() { try { getsmscode(name, pwd); } catch (exception ex) { ex.printstacktrace(); } } }).start(); } } } private void getsmscode(string name, string pwd) { getcode(this.name, this.pwd, new getcodelistener() { @override public void getcodesuccess(string s) { message msg = message.obtain(); msg.obj = s; msg.what = 1; handler.sendmessage(msg); } @override public void getcodefail(string s) { message msg = message.obtain(); msg.obj = s; msg.what = 2; handler.sendmessage(msg); } }); } public class loginclicklistener implements view.onclicklistener { @override public void onclick(view view) { final string code = sms.gettext().tostring().trim(); name = edit_name.gettext().tostring().trim(); pwd = edit_pwd.gettext().tostring().trim(); if (name.equals("")) { edit_name.requestfocus(); } else if (pwd.equals("")) { edit_pwd.requestfocus(); } else if (code.equals("")) { sms.requestfocus(); } else { new thread(new runnable() { @override public void run() { try { checksmscode(name, pwd, code); } catch (exception ex) { ex.printstacktrace(); } } }).start(); } } } private void checksmscode(string name, string pwd, string code) { checkcode(this.name, this.pwd, code, new checkcodelistener() { @override public void checkcodesuccess(string s) { message msg = message.obtain(); msg.obj = s; msg.what = 3; handler.sendmessage(msg); } @override public void checkcodefail(string s) { message msg = message.obtain(); msg.obj = s; msg.what = 4; handler.sendmessage(msg); } }); } }
步骤四(填写配置文件)
1.配置应用权限
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.read_phone_state"/> <uses-permission android:name="android.permission.access_wifi_state"/> <uses-permission android:name="android.permission.access_network_state"/>
2.配置meta (appkey 从平台上新建的对应应用中来)
<meta-data android:name="jpush_appkey" android:value="you jpush_appkey"/>
步骤五(调试)
1.点击获取密码按钮获取短信信息
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。