2016-07-16 153 views
0

我的应用程序的设计,当它活动时,它应该发出1短信收到一个电话/短信。 虽然有些人,多个短信收到1电话/短信收到发出。 我不知道为什么会发生这种情况?它似乎只发生在某些人身上,但我知道这可能是所有用户都可能遇到的潜在错误。应用程序发送多个短信接收电话/短信

任何帮助将不胜感激。

我需要在某处放置if +共享pref布尔值吗?

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); 
    } 

    } 
} 
+0

你是不是检查'phoneNumber'为空在IntentService,可能会产生问题的路线。 –

回答

0

IntentService确实在单个线程的工作。如果您的广播在您的CHmessageSent设置为true之前快速连续调用两次,则会发送2封邮件。

我建议您将这张支票到IntentService内:

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

感谢您的回答。我现在试试看。在这种情况下更好.apply();或者.commit(); –

+1

除了'apply()'没有返回值并且速度稍快之外没有真正的区别。 –