Joke Collection Website - Public benefit messages - How to read Android SMS verification code automatically with contentobserve

How to read Android SMS verification code automatically with contentobserve

There are two main ways to get SMS information on android: BroadcastReceiver and database. It is more convenient to get short message information in real time.

Public class SMSReceiver extends BroadcastReceiver{

Private string verifyCode =

public static final String TAG = " SMS receiver ";

public static final String SMS _ RECEIVED _ ACTION = " Android . provider . telephony . SMS _ RECEIVED ";

@ Overlay

Public void onReceive (context, intention) (

if (intent.getAction()。 Equal to (SMS _ SMS_RECEIVED_ACTION)){) (

SMS message[]messages = getmessagesfrominent(intent);

For (short message: message)

Log.i(TAG,message . getoriginatingaddress()+":"+

message . getdisplayoriginatingaddress()+":"+

message . getdisplaymessagebody()+":"+

message . gettimestampmillis());

string SMS content = message . getdisplaymessagebody();

Log.i(TAG,SMS content);

writeFile(SMS content); //Write the short message to SD card.

}

}

}

public final SMS message[]getmessagesfrominent(Intent Intent){

Object[]messages =(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 SMS message [PDU count];

for(int I = 0; I & ltpduCounti++) {

PDUs[I]= pduObjs[I];

msgs[I]= SMS message . createfrompdu(PDUs[I]);

}

Return message;

}

//Write the SMS content into the file on the SD card, which is convenient for pulling the file to the PC, thus facilitating the automation of other platforms such as WWW/WAP.

@SuppressLint("SdCardPath ")

public void writeFile(String str){

string file path = "/mnt/SD card/verify code . txt ";

byte[]bytes = str . getbytes();

Try {

File file = file path;

file . create new file();

file output stream fos = new file output stream(file);

Fos.write (bytes);

fos . close();

}catch(IOException e){

e . printstacktrace();

}

}

In this way, when a short message is received, the content of the short message can be written into a file in the SD card.

Write a method in another java class to read the contents of the file, and intercept the string according to the specific location of the verification code when writing the test case.

A public string read (String str) throws an IOException{

File file = new file (str);

file inputstream fis = new file inputstream(file);

string buffer sb = new string buffer();

BufferedInputStream bis = new buffered InputStream (FIS);

buffered reader read = new buffered reader(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 to verification;

}

Finally, you need to add a statement to the manifest and register the permissions.

& ltreceiver Android:name = " com . cplatform . surf desktop . test . util . SMS receiver " & gt;

& lt intention filter & gt

& ltaction Android:name = " Android . provider . telephony . SMS _ RECEIVED "/& gt;

& lt/intent-filter & gt;

& lt/receiver & gt;

& ltuses-permission Android:name = " Android . permission . receive _ SMS " & gt; & lt/uses-permission & gt;

& ltuses-permission Android:name = " Android . permission . write _ EXTERNAL _ STORAGE " & gt; & lt/uses-permission & gt;

& ltuses-permission Android:name = " Android . permission . read _ SMS "/& gt;

When needed in the test process, the SMS verification code can be obtained in real time.