android怎样实现用代码从数据库获取短信内容等等

android怎样实现用代码从数据库获取短信内容等等,第1张

数据库获取代码如下:

String databaseFilename = DATABASE_PATH + "/" + DATABASE_NAME

File dir = new File(DATABASE_PATH)

if (!dir.exists())

dir.mkdir()

if (!(new File(databaseFilename)).exists()) {

InputStream is = context.getResources().openRawResource(R.raw.jobexam)

FileOutputStream fos = new FileOutputStream(databaseFilename)

byte[] buffer = new byte[8192]

int count = 0

while ((count = is.read(buffer)) >0) {

fos.write(buffer, 0, count)

}

fos.close()

is.close()

}

db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null)

 android上获取短信信息主要有BroadcastReceiver方式与数据库方式,要实时的话就BroadcastReceiver比较方便

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)){

SmsMessage[] messages = getMessagesFromIntent(intent)

for (SmsMessage message : messages){

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

message.getDisplayOriginatingAddress() + " : " +

message.getDisplayMessageBody() + " : " +

message.getTimestampMillis())

String smsContent=message.getDisplayMessageBody()

Log.i(TAG, smsContent)

writeFile(smsContent)//将短信内容写入SD卡

}

}

}

public final SmsMessage[] getMessagesFromIntent(Intent intent){

Object[] messages = (Object[]) intent.getSerializableExtra("pdus")

byte[][] pduObjs = new byte[messages.length][]

for (int i = 0i <messages.lengthi++)

{

pduObjs[i] = (byte[]) messages[i]

}

byte[][] pdus = new byte[pduObjs.length][]

int pduCount = pdus.length

SmsMessage[] msgs = new SmsMessage[pduCount]

for (int i = 0i <pduCounti++){

pdus[i] = pduObjs[i]

msgs[i] = SmsMessage.createFromPdu(pdus[i])

}

return msgs

}

//将短信内容写到SD卡上的文件里,便于将文件pull到PC,这样可方便其它如WWW/WAP平台的自动化

@SuppressLint("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()

}

}

如此当有短信收到时就可以将短信内容写到SD卡中的文件里

在另一个java类中写个读取文件内容的方法,并在写测试用例过程中,将得到的String按验证码的具体位置截取即可。

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

}

最后需要在manifest中增加申明,且注册权限

<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"/>

测试过程中需要用到短信验证码时就可以实时获取了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9993071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存