Project Summary mobile phone number + SMS Verification code Login

Source: Internet
Author: User
<span id="Label3"></p><p><p>first, you need a phone number, many accounts are now set the account name into a mobile phone number, and then click the button to get the phone verification code.<br>second, you need to backstage to your mobile phone text verification interface, The company uses different, this as the front-end, do not need you to consider, you just let you backstage to write a good interface, you directly call on the Good.</p></p><p><p>Activity_login.xml</p></p><pre class="brush:java;collapse:true;;gutter:true;"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:layout_width=" match_parent "android:layout_height=" match_parent "android:orientation=" Vertical "& Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "60dp" android:layout_marg intop= "20dp" android:layout_marginleft= "10dp" android:layout_marginright= "10dp" android:gravity= "cent ER "android:orientation=" horizontal "> <edittext android:id=" @+id/mobile_login "a Ndroid:layout_width= "match_parent" android:layout_height= "40dp" android:layout_marginleft= "10dp" android:hint= "please Enter your phone number" android:textsize= "16sp" android:background= "@null" Androi D:inputtype= "number"/> </LinearLayout> <linearlayout android:layout_width= "match_pare NT "android:layout_height= "50dp" android:layout_marginleft= "10dp" android:layout_marginright= "10dp" android:layout_margint op= "15dp" android:gravity= "center" android:orientation= "horizontal" > <edittext andro id:id= "@+id/yanzhengma" android:layout_width= "0dp" android:layout_height= "40dp" android:l ayout_weight= "4" android:background= "@null" android:layout_marginleft= "10dp" android:hint = "please Enter the verification code" android:textsize= "16sp" android:inputtype= "number"/> <button android:id= "@+id/getyanzhengma1" android:layout_width= "0dp" android:layout_height= "wrap_conte NT "android:layout_weight=" 1.9 "android:textsize=" 13sp "android:gravity=" Center " android:text= "get Verification code" android:layout_gravity= "center"/> </LinearLayout> <b Utton android:id="@+id/login_btn" android:layout_width= "match_parent" android:layout_height= "40dp" android:layout_marg intop= "20dp" android:layout_marginleft= "20dp" android:layout_marginright= "20dp" android:text= "login" Android:textsize= "16sp"/></linearlayout></pre><p><p> Loginactivity.java </p> </p><pre class="brush:java;gutter:true;">public class Loginactivity extends Activity implements view.onclicklistener {private int countseconds = 60;//countdown seconds Private EditText mobile_login, yanzhengma; Private Button getyanzhengma1, login_btn; Private Context mcontext; Private String usersuccess; Private Handler Mcounthandler = new Handler () {@Override public void Handlemessage (Message Msg) { Super.handlemessage (msg); If (countseconds > 0) {--countseconds; Getyanzhengma1.settext ("(" + countseconds + ") After obtaining the verification code"); mcounthandler.sendemptymessagedelayed (0, 1000); } Else {countseconds = 60; Getyanzhengma1.settext ("please re-obtain the verification code"); } } }; Private String userinfomsg; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Mcontext = this; Setcontentview (r.layout.activity_login); Initview (); Initevent (); InitData (); } private void Initview () {mobile_login = (EditText) Findviewbyid (r.id.mobile_login); GETYANZHENGMA1 = (Button) Findviewbyid (r.id.getyanzhengma1); Yanzhengma = (EditText) Findviewbyid (r.id.yanzhengma); LOGIN_BTN = (Button) Findviewbyid (r.id.login_btn); } private void Initevent () {getyanzhengma1.setonclicklistener (this); Login_btn.setonclicklistener (this); } private void InitData () {} @Override public void OnClick (View v) {switch (v.getid ()) {c ASE r.id.getyanzhengma1:if (countseconds = =) {String mobile = Mobile_login.gettext ( ). toString (); LOG.E ("tag", "mobile==" + mobile); Getmobiile (mobile); } else {toast.maketext (loginactivity.this, "verification code cannot be sent repeatedly", toast.length_short). show (); } break; Case R.id.login_bTn:login (); Break default:break; }}//get information to log on public void login () {String mobile = mobile_login.gettext (). toString (). trim (); String Verifycode = yanzhengma.gettext (). toString (). trim (); Requestparams params = new Requestparams ("here is the interface for your request login"); X.http (). Post (params, new callback.progresscallback<string> () {@Override public void Onwaitin G () {} @Override public void onstarted () {} @Override public void Onloading (long, long, boolean Isdownloading) {} @Override Publ IC void onsuccess (String result) {try {jsonobject jsonobject = new Jsonobject (result); LOG.E ("tag", "landed result=" + jsonobject); String success = Jsonobject.optstring ("success"); String data = Jsonobject.optstring ("data"); String msg=jsonobject.optstring ("msg"); If ("true". equals (SUCCESS)) {log.e ("tag", "landed data=" +data); Jsonobject json = new Jsonobject (data); token = json.optstring ("token"); UserID = json.optstring ("userid"); I wrote here according to my request, you can also appropriately change//get the status of user information GetUserInfo (); }else{toast.maketext (loginactivity.this, msg, toast.length_short). show (); }} catch (jsonexception e) {e.printstacktrace (); }} @Override public void OnError (throwable ex, boolean isoncallback) {} @Override public void oncancelled (cancelledexception Cex) {} @Override public void onfinished () { } }); }//get Verification code information to determine if there is a mobile number private void Getmobiile (String mobile) {if ("". equals (mobile)) {log.e ("tag "," mobile= "+ mobile); New Alertdialog.builder (mcontext). settitle ("hint"). setmessage ("mobile phone number cannot be empty"). setcancelable (true). show (); } else if (ismobileno (mobile) = = False) {new alertdialog.builder (mcontext). settitle ("hint"). setmessage ("please Enter the correct phone Number "). setcancelable (true). show (); } else {LOG.E ("tag", "enter the correct phone number"); Requestverifycode (mobile); }}//get Verification Code information, make CAPTCHA request private void Requestverifycode (String mobile) {requestparams requestparams = new Reque Stparams ("here is the verification code you requested interface, let backstage to you, parameters, etc. added in the back"); X.http (). Post (requestparams, new callback.progresscallback<string> () {@Override public void O Nwaiting () {} @Override public void onstarted () {} @Override public void onloading (long total, long Current, boolean Isdownloading) {} @Override public void onsuccess (String Result) { Try {jsonobject jsonObject2 = new Jsonobject (result); LOG.E ("tag", "jsonObject2" + jsonObject2); String state = jsonobject2.getstring ("success"); String Verifycode = jsonobject2.getstring ("msg"); LOG.E ("tag", "get Verification code = =" + verifycode); If ("true". equals (state)) {toast.maketext (loginactivity.this, verifycode, toast.length_short). Show (); Startcountback ();//this is the} else {toast.maketext (loginactivity.this, Veri) that is used to request Parameters. fycode, toast.length_short). Show (); }} catch (jsonexception e) {e.printstacktrace (); }} @Override public void OnError (throwable ex, boolean isOnCallback) {ex.printstacktrace (); } @Override public void oncancelled (cancelledexception Cex) {} @Override public void onfinished () {}}); }//use Regular expressions to determine the phone number public static Boolean Ismobileno (String tel) {Pattern p = pattern.compile ("^ (13[0-9]|15 ([0 -3]| [5-9]) | 14[5,7,9]|17[1,3,5,6,7,8]|18[0-9]) \\d{8}$ "); Matcher m = P.matcher (tel); System.out.println (m.matches () + "---"); return m.matches (); }//get Verification Code information, take the timer operation private void Startcountback () {(Activity) mcontext). runonuithread (new Runnable () { @Override public void Run () {getyanzhengma1.settext (countseconds + ""); Mcounthandler.sendemptymessage (0); } }); }}</pre><p><p>  </p></p><p><p>Project Summary mobile phone number + SMS Verification code Login</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.