如何从以编程方式在Android的收件箱读取短信?收件箱、短信、方式、Android

2023-09-11 10:42:31 作者:薄情痞子

我要检索从收件箱中的短信并显示出来?

I want to retrieve the SMS messages from the inbox and display them?

推荐答案

使用内容解析器(的内容://短信/收件箱)。阅读短信这是在收件箱

Use Content Resolver ("content://sms/inbox") to read SMS which are in inbox.

// public static final String INBOX = "content://sms/inbox";
// public static final String SENT = "content://sms/sent";
// public static final String DRAFT = "content://sms/draft";
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);

if (cursor.moveToFirst()) { // must check the result to prevent exception
    do {
       String msgData = "";
       for(int idx=0;idx<cursor.getColumnCount();idx++)
       {
           msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
       }
       // use msgData
    } while (cursor.moveToNext());
} else {
   // empty box, no SMS
}

请添加 READ_SMS 许可。

我希望它可以帮助:)