2016-07-16 15 views
0

私のアプリは、アクティブなときには、1通のSMS/SMS受信時に送信するように設計されています。いくつかの人々のために 、複数のSMSは1通話/ SMSの受信で送信されます。 なぜこれが起こっているのかわかりませんか?それは一部の人に起こるように見えますが、それはすべてのユーザーに起こる可能性のあるバグである可能性があることを認識しています。通話/ SMSの受信時に複数のSMSを送信するアプリケーション

ご迷惑をおかけして申し訳ございません。

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

    } 
} 
+0

をあなたはラインの下の問題を作成するかもしれない、あなたのIntentServiceにnullを 'phoneNumber'をチェックされていません。 –

答えて

0

IntentServiceは、単一のスレッド上で作業を行います。 CHmessageSentがtrueに設定される前にブロードキャストが2回連続して呼び出されると、2つのメッセージが送信されます。

私はあなたがIntentService内側には、このチェックを移動するお勧めします:

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

あなたの答えに感謝します。病気は今これを試してください。このケースではより良い.apply()です。または.commit(); –

+1

'apply()'が値を返さず、少し速いという点を除いて、実際の違いはありません。 –

関連する問題