You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SMS-MMS类返回数据异常求助:返回6倍预期数据

Hey Jason, let's dig into this SMS-MMS duplication issue you're facing—it sounds frustrating to get 6x the data you need (3 old + 3 current messages) when you only want the latest single message. Let's break down possible causes and fixes based on what you've shared:

1. Check for Duplicate Observer Registrations

Your debug log shows Observer Started—is it possible your SMS observer is being registered multiple times? For example, if you're registering it in onResume without unregistering properly, every time your activity comes to the foreground, a new observer gets attached. This would make every incoming message trigger multiple callbacks, leading to duplicated data entries.

  • Fix: Register the observer once (e.g., in onCreate) and unregister it in onDestroy to avoid duplicates. Add a check to ensure you don't register an already active observer.

2. Filter SMS Type & Limit Results

Your log notes Type: 1 (which is Android's TYPE_INBOX for incoming SMS). If your query isn't filtering by message type or limiting results, you might be pulling in duplicate entries from different message folders (like sent messages, drafts, or archived threads) alongside the new message. Combine that with duplicate callbacks, and you get the 6x multiplier.

  • Fix: Add strict filters to your ContentResolver query to target only the message type you need, sort by date descending, and limit to just the latest entry:
// Example Android SMS query with filters
String selection = Telephony.Sms.TYPE + " = ?";
String[] selectionArgs = new String[]{String.valueOf(Telephony.Sms.TYPE_INBOX)};
String sortOrder = Telephony.Sms.DATE + " DESC LIMIT 1"; // Only get the newest message

Cursor cursor = getContentResolver().query(
    Telephony.Sms.CONTENT_URI,
    null,
    selection,
    selectionArgs,
    sortOrder
);

if (cursor != null && cursor.moveToFirst()) {
    // Process only this single latest message
    String from = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS));
    String body = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.BODY));
    // ... rest of your processing logic
    cursor.close();
}

3. Audit BroadcastReceiver Setup

If you're using a BroadcastReceiver to listen for SMS, make sure you haven't registered it twice—once in the manifest and once dynamically in code. Double-registration causes the receiver to fire multiple times for the same message.

  • Fix: Stick to one registration method: either declare it in the manifest (with android:exported="false" if you don't need external triggers) or register it dynamically in your component, and unregister it when the component is destroyed.

4. Validate Your SMS-MMS Class Logic

Since you've rebuilt the class source, double-check these details:

  • Are you clearing your dataset before adding new messages? If you're appending to a list without resetting it, old entries will stick around alongside new ones.
  • Is there accidental loop duplication in your cursor processing? For example, a nested loop that iterates over the same cursor multiple times, adding the same message repeatedly.

5. Test in a Clean Environment

Sometimes emulators or test devices hold cached broadcast events or leftover observer instances. Try restarting your device, clearing your app's cache, and running a fresh test to rule out stale data causing duplicates.

内容的提问来源于stack exchange,提问作者Jason Edel-Brock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:19:03