Android gets SMS verification code and automatically fills in the implementation of a

Source: Internet
Author: User
<span id="Label3"></p>Android gets SMS verification code and automatically fills in the implementation of a<span class="p-time"><span class="p-time"><span class="p-author"><span class="p-view"> </span></span></span></span><p><p>Android Get SMS Verification code and automatically fill in the implementation of one, there is a need for friends can refer to the Next.</p></p><p><p></p></p><p><p>Now the application in the registration login or change password is used in the SMS verification code, that in Android How to achieve the SMS Verification code and automatically fill it?</p></p><p><p>first, you need to register the right to receive and read text messages in Manifest:</p></p><p><p></p></p><pre><pre> <uses-permission android:name= "android.permission.RECEIVE_SMS" ></uses-permission> < Uses-permission android:name= "android.permission.READ_SMS"/></pre></pre><p><p><br>Implement a broadcast smsbroadcastreceiver to listen for text messages:</p></p><p><p></p></p><pre>Package Com.example.receive;import Java.text.simpledateformat;import Java.util.date;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.telephony.smsmessage;/** * SMS Monitor * @author * */public class Smsbroadcastreceiver extends Broadcastreceiver {PR Ivate Static MessageListener mmessagelistener; public static final String sms_received_action = "android.provider.Telephony.SMS_RECEIVED";p ublic Smsbroadcastreceiver () {super (); } @Overridepublic void OnReceive (context context, Intent Intent) {if (intent.getaction (). equals (sms_received_action ) {object[] PDUs = (object[]) Intent.getextras (). get ("pdus"); For (Object pdu:pdus) {smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU ((byte []) pdu); String sender = Smsmessage.getdisplayoriginatingaddress (); //<span id="1_nwp" style="width: auto; height: auto; float: none;"><span style="color: #0000ff; font-size: 12px; width: auto; height: auto; float: none;"><span style="color: #0000ff; font-size: 12px; width: auto; height: auto; float: none;">SMS Content</span></span>String content = Smsmessage.getdisplaymessagebody ();<span id="2_nwp" style="width: auto; height: auto; float: none;"><span id="2_nwp" style="width: auto; height: auto; float: none;"> <span style="color: #0000ff; font-size: 12px; width: auto; height: auto; float: none;">long</span> date = Smsmessage.gettimestampmillis (); Date tiemdate = new Date (date); SimpleDateFormat SimpleDateFormat = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss"); String time = Simpledateformat.format (tiemdate); Filter the Send number if ("+8613450214963") for text messages that do not need to be read. equals (sender) {mmessagelistener.onreceived (con tent); Abortbroadcast (); }}}}//callback interface public interface MessageListener {public void onreceived (String message); } public void Setonreceivedmessagelistener (messagelistener messagelistener) {this.mmessagelistener = Mess agelistener; }}</span></span></span></pre><p><p><br>In the activity that needs to fill in the verification code, the production Smsbroadcastreceiver instance, realizes the onreceived callback Interface. In order to conserve system resources, we use dynamic registration to unregister the broadcast Method.</p></p><pre>Package Com.example.smstest;import Com.example.receive.smsbroadcastreceiver;import Android.os.bundle;import Android.app.activity;import Android.content.intentfilter;import Android.view.menu;import android.widget.EditText; public class Mainactivity extends Activity {private EditText edtpassword;private smsbroadcastreceiver Msmsbroadcastreceiver;private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_main); Edtpassword = (EditText) Findviewbyid (r.id.password);} @Overrideprotected void OnStart () {super.onstart (); Generate broadcast processing msmsbroadcastreceiver = new Smsbroadcastreceiver (); Instantiation of<span id="0_nwp" style="width: auto; height: auto; float: none;"><span id="0_nwp" style="width: auto; height: auto; float: none;"> <span style="color: #0000ff; font-size: 12px; width: auto; height: auto; float: none;">filter</span> and set the broadcast to be filtered intentfilter intentfilter = new Intentfilter (ACTION); Intentfilter.setpriority (integer.max_value); Registered broadcast This.registerreceiver (msmsbroadcastreceiver, intentfilter); Msmsbroadcastreceiver.setonreceivedmessagelistener (new Smsbroadcastreceiver.messagelistener () {@Override public void onreceived (String Message) {edtpassword.settext (message); } }); } @Overrideprotected void OnDestroy () {super.ondestroy ();//logoff SMS monitoring broadcast This.unregisterreceiver (msmsbroadcastreceiver); }}</span></span></pre><p><p>Android gets SMS verification code and automatically fills in the implementation of a</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.