我想知道,如何以編程方式讀取特定號碼的短消息? 我知道如何使用內容提供商,但不知道我是否應該使用「人」欄或「地址」欄或完全不同的方式來讀取短信 請幫 感謝獲取特定電話號碼的短信
2
A
回答
5
它會列出消息從指定的數。
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
while (cursor1.moveToNext()){
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if(address.equalsIgnoreCase("number")){ //put your number here
String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
String body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
Log.d("*******", "body="+body);
}
}
}
但我遇到"content://mms-sms/conversations/"
我認爲這將返回的幫助thread_id
具體數量直接整個對話來了,檢查this
0
public class SmsReceiver extends BroadcastReceiver {
String specificPhoneNumber = "No you want";
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String phNum = msgs[i].getOriginatingAddress();
str += msgs[i].getMessageBody().toString();
if (specificPhoneNumber.equals(phNum))
{
Uri uri = Uri.parse("content://sms/inbox");
ContentResolver contentResolver = context.getContentResolver();
String where = "address="+phNum;
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "thread_id"}, where, null,
null);
while (cursor.moveToNext()) {
long thread_id = cursor.getLong(1);
where = "thread_id="+thread_id;
Uri thread = Uri.parse("content://sms/inbox");
context.getContentResolver().delete(thread, where, null);
}
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",str);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}
}
}
}
}
+0
@Snake只是檢查我的答案.. – Rahil2952
1
您可以使用selectionArgs兩個更有效率:
String[] phoneNumber = new String[] { "+18839494492" }; //the wanted phone number
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "address=?", phoneNumber, null);
有了這個改變,你只能從想要的號碼中獲得短信,並且你不必抓取所有收到的短信。
相關問題
- 1. 獲取短信/彩信的電話號碼
- 2. 通過內容獲取每條短信的電話號碼://短信/
- 3. Android:如何從傳入的短信中獲取電話號碼?
- 4. 獲取短信發件人的電話號碼,使用Twilio
- 5. 如何獲取設備上每條短信的電話號碼?
- 6. 如何從特定電話號碼接收短信?
- 7. 從特定號碼讀取短信
- 8. 如何從短信中獲取電話號碼android
- 9. MFMessageComposeViewController傳送應用程式短信獲取電話號碼
- 10. 更改電話號碼發短信
- 11. 基於短信會話的自定義IME電話號碼
- 12. 獲取所有短信/彩信發送到Android中的一個特定的電話號碼
- 13. 如何獲取撥打電話的電話號碼信息
- 14. 從電話號碼是無效的短信能力的入站電話號碼
- 15. 如何獲得傳入短信的電話號碼?
- 16. 從iphone中的指定電話號碼阻止呼叫/短信?
- 17. 獲取電話號碼
- 18. Android SDK-獲取特定聯繫人的電話號碼
- 19. 如何獲取特定羣組的電話號碼
- 20. 確定電話號碼是否在Android中有短信息
- 21. arduino和GSM模塊sim900如何獲取短信的電話號碼?
- 22. 如何在Android中獲取短信收件人的電話號碼
- 23. Android:從特定號碼接收短信?
- 24. 未收到短信中的電話號碼和電話號碼中的正確號碼
- 25. 獲取單個電話號碼出站短信使用情況/日誌
- 26. 獲取短信和電話號碼,並將其傳送到另一個活動
- 27. 如何讀取特定的手機號碼,短信中的Android
- 28. 從電話號碼獲取區號
- 29. 安卓:從特定的電話號碼
- 30. 如何使用javascript在電話號碼中獲取特定號碼
你好,感謝您的答案,但這並不回答我的問題。在這裏,你在說我如何準備好收到的短信。我的問題是,我有一個特定的號碼「xxxxxxxx」,我怎樣才能從收件箱中獲得這個號碼發送的所有消息。所以沒有什麼與onReceive相關,因爲它是一個閱讀收件箱短信並通過它們嘗試查找特定電話號碼的短信的活動。 – Snake
我聞你有雙卡,現在你想要獲取所有發送的信息具體的號碼,但這句話並不清楚,你怎麼才能從收件箱中獲得這個號碼發送的所有消息。由於收件箱包含收到的郵件未發送,並且您希望從收件箱發送郵件,因此收件箱沒有任何意義。 – RobinHood
我不是指「已發送」消息。我指的是「收到的消息」。如果你仔細閱讀我的評論,你會發現我問的是發送「BY」這個號碼的消息不是「To」。換句話說,我在尋找從這個號碼收到的消息。我不想收聽目前收到的消息,而是收聽過去收到的消息。例如,我想知道昨天從號碼xxxx收到的消息。 – Snake