[英]How to get last sms from the device?
我一直在開發必須識別SMS命令的應用程序。 所以,我需要知道如何獲得最后的短信。 我知道我需要使用BroadcastReceiver(我使用它),但我不知道如何在這個類中獲得最后的短信。 請幫助我,我希望你能做到。 先感謝您。
感謝Bo獲取我博客的鏈接。
要從嵌入式android數據庫(sqlite)獲取最后一條消息,首先要創建一個游標實例。
Cursor cursor = context.getContentResolver().query("content://sms", null, null, null, null);
然后移動到第一個短信(第一個是最后一個接收者;))
cursor.moveToFirst();
看看我的博客我是怎么做的,就像博告訴你的那樣;)
//刪除了鏈接
干杯
看下面的代碼可能對你有幫助。
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = con.getContentResolver();
Cursor c = cr.query(myMessage, new String[] { "_id",
"address", "date", "body", "read" }, null,
null, null);
startManagingCursor(c);
Main_calss.getSmsLogs(c, con);
public static final ArrayList<String> sms_num = new ArrayList<String>();
public static final ArrayList<String> sms_body = new ArrayList<String>();
public static void getSmsLogs(Cursor c, Context con) {
if (sms_num.size() > 0) {
sms_num.clear();
sms_body.clear();
}
try {
if (c.moveToFirst()) {
do {
Log.d("error",
""
+ c.getString(c
.getColumnIndexOrThrow("address")));
if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
c.moveToNext();
continue;
}
String Number = c.getString(
c.getColumnIndexOrThrow("address")).toString();
String Body = c.getString(c.getColumnIndexOrThrow("body"))
.toString();
sms_num.add(Number);
sms_body.add(Body);
} while (c.moveToNext());
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
現在得到最后的按摩,你必須這樣做。
sms_num.get(sms_num.size()-1);
sms_body.get(sms_num.size()-1);
如果它是正確的那么就做對了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.