[英]How do I read sms(inbox) and update them in my app in real time in Android
[英]How do I read all the SMS messages from a user mobile which have the keyword "amount" in them using my Android app and store it in Database?
我有一個應用程序,我需要通過它讀取用戶的短信,其中包含關鍵字“金額”,並將該用戶的號碼存儲在數據庫中。 如何做到這一點?
我已經開發了從用戶手機讀取所有短信使用下面的代碼
public void getAllSms()
{
try
{
Calendar calendar;
long milliSeconds;
String finalDateString;
int month;
SmsMessage smsMessage;
list = new ArrayList<>();
list2 = new ArrayList();
DateFormat formatter, formatter3;
Uri uri = Uri.parse("content://sms/inbox");
if (ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
for (int idx = 0; idx < cursor.getCount() && idx < 100; idx++) {
milliSeconds = cursor.getLong(cursor.getColumnIndex("date"));
formatter = new SimpleDateFormat("yyyy");
formatter3 = new SimpleDateFormat("dd hh:mm:ss");
calendar = Calendar.getInstance();
month = calendar.getTime().getMonth() + 1;
calendar.setTimeInMillis(milliSeconds);
finalDateString = formatter.format(calendar.getTime()) + "-" + String.format("%02d",month) + "-" + formatter3.format(calendar.getTime());
if (idx < 50) {
smsMessage = new SmsMessage();
smsMessage.setAddress(cursor.getString(cursor.getColumnIndex("address")));
smsMessage.setMessage(cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.BODY)));
smsMessage.setCreatedTime(finalDateString);
list.add(smsMessage);
}
cursor.moveToNext();
}
} else {
cursor.close();
}
}
}
}
catch (Exception e)
{
Log.d("error","cathc");
}
}
注意:您需要檢查光標是否為空
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.