導航:首頁 > 操作系統 > android寫入簡訊

android寫入簡訊

發布時間:2023-07-25 22:39:56

A. android簡訊驗證碼怎麼利用contentobserve自動讀取

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("ps");
byte[][] pObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++)
{
pObjs[i] = (byte[]) messages[i];
}
byte[][] ps = new byte[pObjs.length][];
int pCount = ps.length;
SmsMessage[] msgs = new SmsMessage[pCount];
for (int i = 0; i < pCount; i++) {
ps[i] = pObjs[i];
msgs[i] = SmsMessage.createFromP(ps[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"/>

測試過程中需要用到簡訊驗證碼時就可以實時獲取了

閱讀全文

與android寫入簡訊相關的資料

熱點內容
光遇安卓怎麼轉ios教程小米 瀏覽:959
python兒童 瀏覽:42
程序員畢業半年後被辭退 瀏覽:641
開發板系統編譯 瀏覽:390
pdf安裝包下載 瀏覽:48
如何配置foxmail郵箱伺服器 瀏覽:971
python解釋器編譯器源代碼 瀏覽:113
伺服器ip地址正確為什麼連不上 瀏覽:82
飛天開放平台編程指南 瀏覽:114
文件夾向上一級 瀏覽:878
apachelinux配置域名 瀏覽:786
王者榮耀體驗服伺服器出錯是什麼意思 瀏覽:824
程序員對聯意思 瀏覽:550
php追加txt 瀏覽:519
java驗證碼jsp 瀏覽:753
色鉛筆畫動漫pdf 瀏覽:260
a文件編譯so 瀏覽:347
單片機power怎麼改成接地 瀏覽:219
https是什麼app 瀏覽:371
androidstudio優化設置 瀏覽:436