为什么有时会调用SMS BroadcastReceiver两次?

时间:2014-03-01 00:27:27

标签: android

我为android.provider.Telephony.SMS_RECEIVED注册了BroadcastReceiver。

有时,BroadcastReceiver onReceive会在同一条短信上被调用两次。

为什么?

我做错了什么?

private void initSmsReceivers() {
smsReceiver = new SmsReceiver();
getActivity().registerReceiver(smsReceiver,
            new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}



public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                // str += "SMS from " + msgs[i].getOriginatingAddress();
                // str += " :";
                str += msgs[i].getMessageBody().toString();

            }
            // ---display the new SMS message---
            String output = CryptoUtils.decrypt(str);
            Toast.makeText(context, output, Toast.LENGTH_LONG).show();

            try {
                parsemessage(output);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.e("JSON2", e.toString());
            }

        }
    }
}

3 个答案:

答案 0 :(得分:4)

可能是您以以编程方式静态注册广播接收器。因此,请确保initSmsReceivers只被调用一次,并且在清单中您没有重新注册该接收者!

以下是如何在清单中静态注册sms的接收器:

<receiver
            android:name="your.package.SmsReceiver"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS" >
            <intent-filter android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
</receiver>

答案 1 :(得分:1)

我在做行动之前通过过滤解决了我的问题。在我的情况下,接收器被调用两次,因为我有两个意图过滤器'SMS_DELIVER'和'SMS_RECEIVED'..请参阅下面的示例

在我的清单中

<receiver
        android:name=".SmsReceiver"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_DELIVER" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

广播接收器

public class SmsReceiver extends BroadcastReceiver {
String TAG = "SmsReceiver";
String sender, text;
private static final String ACTION_SMS_NEW = "android.provider.Telephony.SMS_DELIVER";

@Override
public void onReceive(Context context, Intent intent) {
    // ---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        SmsMessage[] msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

            sender = msgs[i].getOriginatingAddress();
            text += msgs[i].getMessageBody().toString();
            Log.i(TAG, sender + " sender\ntext " + text);
        }
        final String action = intent.getAction();
        Log.d(TAG, "onReceive(context, " + action + ")");
        if (ACTION_SMS_NEW.equals(action)) {

            ContentValues values = new ContentValues();
            values.put("address", sender);
            values.put("body", text);
            context.getContentResolver().insert(
                    Uri.parse("content://sms/inbox"), values);

            MiscMethods.delay(10);
        }
    }

}

}

答案 2 :(得分:0)

onReceive()过滤器意图操作中:

if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"){
       //handle received sms
}
else if(intent.getAction().equals("android.provider.Telephony.SMS_DELIVER"){
       //do something
}