我的应用程序是设计的,当活动时,它应该发送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);
}
}
}发布于 2016-07-16 13:35:55
IntentService确实在一个线程上工作。如果您的广播在CHmessageSent设置为true之前连续调用两次,则将发送2条消息。
我建议您将此支票移到IntentService中:
Boolean messageSent = preferences.getBoolean("CHmessageSent", false);
if (!messageSent) {
//Send SMS
editor.putBoolean("CHmessageSent", true).apply();
}https://stackoverflow.com/questions/38411419
复制相似问题