Android applications of automated testing will involve registering login function, and many of the registration or modify the password function often need to enter the SMS authentication code, so it is necessary to automatically obtain the Issued SMS authentication code. The
is mostly to get SMS messages in real time .
Android access to SMS messages are mainly Broadcastreceiver way and database mode, to real-time words on the broadcastreceiver more convenient, share an article you can view, click.
public class Smsreceiver extends broadcastreceiver{private String verifycode= "";
public static final String TAG = "Smsreceiver";
public static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED"; @Override public void OnReceive (context context, Intent Intent) {if (Intent.getaction (). Equals (Sms_received_action)) {S
msmessage[] messages = getmessagesfromintent (intent); for (Smsmessage message:messages) {log.i (TAG, message.getoriginatingaddress () + ":" + Message.getdisplayoriginatinga
Ddress () + ":" + message.getdisplaymessagebody () + ":" + Message.gettimestampmillis ());
String Smscontent=message.getdisplaymessagebody ();
LOG.I (TAG, smscontent); WriteFile (smscontent)//write SMS content to SD card}} public final smsmessage[] Getmessagesfromintent (Intent Intent) {object[] Me
Ssages = (object[]) Intent.getserializableextra ("PDUs");
byte[][] Pduobjs = new byte[messages.length][]; for (int i = 0; i < messages.length i++) {pduobjs[i] = (byte[]) MeSsages[i];
} byte[][] PDUs = new byte[pduobjs.length][];
int pducount = Pdus.length;
smsmessage[] msgs = new Smsmessage[pducount];
for (int i = 0; i < Pducount i++) {pdus[i] = pduobjs[i];
Msgs[i] = SMSMESSAGE.CREATEFROMPDU (Pdus[i]);
return msgs; //write SMS content to the file on SD card to facilitate the pull of files to the PC, which facilitates the automation of other Www/wap platforms ("Sdcardpath") public void WriteFile (String str)
{String filepath= "/mnt/sdcard/verifycode.txt";
byte [] bytes = Str.getbytes ();
try{file File=new file (FilePath);
File.createnewfile ();
FileOutputStream fos=new fileoutputstream (file);
Fos.write (bytes);
Fos.close ();
}catch (IOException e) {e.printstacktrace ();
}
}
So when a text message is received, it can be written to the file in the SD card.
Write a method to read the contents of a file in another Java class and, in the process of writing a test case, intercept the resulting string by the exact location of the verification code.
Public String Read (String str) throws ioexception{
file File=new file (str);
FileInputStream fis=new fileinputstream (file);
StringBuffer sb=new StringBuffer ();
Bufferedinputstream bis=new bufferedinputstream (FIS);
BufferedReader read = new BufferedReader (new InputStreamReader (bis));
int c=0;
while ((C=read.read ())!=-1) {
sb.append ((char) c);
}
Read.close ();
Bis.close ();
Fis.close ();
LOG.I (TAG, sb.tostring ());
String verify=sb.tostring ();
return verify;
}
Finally, you need to add the declaration in the manifest and register permissions
<receiver android:name= "Com.cplatform.surfdesktop.test.util.SMSReceiver" >
<intent-filter>
<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</ receiver>
<uses-permission android:name= "Android.permission.RECEIVE_SMS" ></uses-permission>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name= "Android.permission.READ_SMS"/>
In the test process, you need to use the SMS verification code can be real-time access.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.