应用程序在收到呼叫/短信时发送多个短信

时间:2016-07-16 12:53:04

标签: java android broadcastreceiver sms intentservice

我的应用程序设计为在激活时它应该在收到呼叫/短信时发送1个短信。 但是对于某些人来说,收到1个电话/短信时会发出多个短信。 我不知道为什么会这样?它似乎只发生在一些人身上,但我知道它可能是一个可能发生在所有用户身上的潜在错误。

任何帮助将不胜感激。

我是否需要在某处添加if + shared pref boolean?

SmsReceiver

public class SmsReceiver extends BroadcastReceiver {

private String tempMessage = "";


@Override
// when OnRecieve recieves the correct Broadcast. in this case when a sms is recieved
public void onReceive(Context context, Intent intent) {


    String action = intent.getAction();

    //Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();


    if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
        //action for sms received


        // the actual sms will come in the form of a a intent
        final Bundle bundle = intent.getExtras();


        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();


                    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                    SharedPreferences.Editor editor = preferences.edit();

                    editor.putString("incomingNumber", phoneNumber);
                    editor.commit();

                    String message = currentMessage.getDisplayMessageBody();



                    if (!tempMessage.equalsIgnoreCase(message)) {


                        if (phoneNumber.contains("+")) {


                            //TODO after receiver is finished set CHmessageSent pref boolean to false.
                            Boolean messageSent = preferences.getBoolean("CHmessageSent", false);
                            if (!messageSent) {

                                Intent smsIntent = new Intent(context, sendSmsIntentService.class);
                                context.startService(smsIntent);
                                //Toast.makeText(context, "startIntent", Toast.LENGTH_SHORT).show();
                            }


                            Log.i("SMS_RECEIVER", "senderNumA: " + phoneNumber + "; message: " + message);


                        }
                    }   

                } // End For loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReciever", "Exeption smsReceiver" + e);
        }
    } // END IF ction.equals("android.provider.Telephony.SMS_RECEIVED")

   }

 }

SendSmsIntentService

public class sendSmsIntentService extends IntentService {


private String phoneNumber;
private String defaultSms = "";
private String sms;


//Creates an IntentService.  Invoked by your subclass's constructor.

public sendSmsIntentService() {
    super("sendSmsIntentService");
}


@Override
protected void onHandleIntent(Intent intent) {


    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();

    phoneNumber = preferences.getString("incomingNumber", "null");



    defaultSms = getString(R.string.drivesafesms);
    sms = preferences.getString("message1",defaultSms);

    Log.i("SMS_RECEIVER", "senderNumb: " + phoneNumber);

    //


    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, sms, null, null);


        if (!preferences.getBoolean("CHmessageSent",false)) {
            editor.putBoolean("CHmessageSent", true);
            editor.commit();
        }

        //Toast.makeText(getApplicationContext(), R.string.receivedCall, Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), R.string.smsFailed, Toast.LENGTH_LONG).show();
        Log.i("CALL_RECEIVER", "senderNum: " + phoneNumber);
    }

   }
}

1 个答案:

答案 0 :(得分:0)

IntentService可以在单个线程上运行。如果在CHmessageSent设置为true之前快速连续两次调用广播,则会发送2条消息。

我建议您将此检查移至IntentService内部:

Boolean messageSent = preferences.getBoolean("CHmessageSent", false);
if (!messageSent) {
    //Send SMS
editor.putBoolean("CHmessageSent", true).apply();
}