Recently in the interface test encountered a problem, is that there is a very important interface to use the SMS Verification code, and other interfaces are dependent on this verification code, if no SMS verification code can not test the following interface, so in order to verify the timing of the interface is normal, and do not modify the code, So just think about the solution, if you have a better plan to share with each other
Android Code
Android sends an action-Android.provider.Telephony.SMS_RECEIVED broadcast after it receives a text message, so we just need to write a class-inheriting broadcastreceiver to easily listen to the SMS.
- Package Com.example.getsms;import Android.content.broadcastreceiver;import Android.content.contentresolver;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.telephony.smsmessage;import Android.text.textutils;import Android.util.log;public Class Smsinterceptreceiver extends Broadcastreceiver {private final String TAG = "Smsrec";private static final String Sms_extra_name = "PDUs";@Overridepublic void OnReceive (context context, Intent Intent) {TODO auto-generated Method StubString message = "";LOG.E (TAG, "free message");Bundle extras = Intent.getextras (); if (extras! = null) {try { object[] Smsextra = (object[]) extras.get (sms_extra_name); &NBSP ; Contentresolver contentresolver = Context.getcontentresolver (); & nbsp &NBSP;LOG.E (TAG, "free message"); &NB Sp for (int i = 0; i < smsextra.length; ++i) { &NBSP ; Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU ((byte[]) smsextra[i]); &N Bsp string BODY = sms.getmessagebody () Tostri Ng (); message + body; &NBS P } &NBS P &NBSP;LOG.E (TAG, "free message:" + message); &NBS P } catch (Exception e) { Todo:handle exceptionLOG.E (TAG, E.getmessage ()); } }}}
androidmanifest.xml Register the receiver:
- <receiver Android:name= ". Smsinterceptreceiver "> <intent-filter> <action android:name= " Android.provider.Telephony.SMS_RECEIVED "/> < /intent-filter> </RECEIVER>
Add Permissions:
- <uses-permission Android:name="Android.permission.RECEIVE_SMS"/>
Python code, mainly through the ADB log to get the SMS information captured by the APK package, and then after the analysis can be used
__author__ = ' Guozhenhua '
#coding =utf-8
Import Urllib2
Import Os,time
#解析短信验证码
Os.system ("adb logcat-c")
cmd= "adb logcat-d |findstr E/smsrec"
#time. Sleep (30);
while (1):
Smscode= os.popen (cmd). Read ()
#print Smscode
if (smscode!= ""):
Smscode=smscode.split ("CAPTCHA:") [1].split (",") [0]
Break
Print "Verification code is:" +smscode
Python solution interface test to get phone verification code problem