这可能是由于您的应用程序为同一条短信注册了两个广播接收器所导致的。如果您已经在AndroidManifest.xml中注册了多个接收器,请确保只在一个接收器中处理短信。您可以通过在接收器onReceive()方法内增加日志语句查看接收器是否调用了多次。以下是一个简单的示例代码,您可以将其添加到您的接收器中进行测试:
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "SmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Sms Received");
// Handle SMS here...
}
}
此外,您还可以通过检查短信的时间戳和内容来过滤掉重复的短信。可以将最后接收到的时间戳和内容存储在SharedPreferences中,并在每次接收短信时进行比较。以下是一个简单的示例代码:
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "SmsReceiver";
private static final String PREFS_NAME = "sms_prefs";
private static final String PREF_LAST_SMS_TIME = "last_sms_time";
private static final String PREF_LAST_SMS_CONTENT = "last_sms_content";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Sms Received");
// Get SMS timestamp and content
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
long timestamp = messages[0].getTimestampMillis();
String content = messages[0].getMessageBody();
// Check if this is a duplicate SMS
SharedPreferences prefs = context.getSharedPreferences(P
上一篇:Android短信读取权限问题
下一篇:Android短信权限声明表格