Search code examples
androidsmsmanager

SMS manager keeps sending SMS repeatedly


I have created a short code in a service to send text messages but the same sms keeps being sent over and over ntil I stop the phone. So here am I in a loop of death...Same goes for the calls.

Here are code snippets :

public int onStartCommand(final Intent intent, final int flags,
                final int startId) {


            if (activer == 0 && activerTransmission == 1 && activerCascade == 0){
                sendSms(getApplicationContext());
                stopSelf();
            }

            if (activer == 0 && activerTransmission == 0 && activerCascade == 1){
                call(getApplicationContext());
                stopSelf();
            }
            if (activer == 0 && activerTransmission == 1 && activerCascade == 1){
                sendSms(getApplicationContext());
                call(getApplicationContext());
                stopSelf();
                }               
            else if (activer == 1){

                Intent dialogIntent = new Intent(getBaseContext(), DialogLauncher.class);
                dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplication().startActivity(dialogIntent);
                stopSelf();     
        }
             return START_NOT_STICKY;
     }


public void sendSms(Context context){

        DBAdapter   smsBDD          =   new DBAdapter(context);     
        Cursor      sms             =   smsBDD.selectTransmission();

        String      numero1         =   (sms.getString(sms.getColumnIndex("sms1")));
        String      numero2         =   (sms.getString(sms.getColumnIndex("sms2")));
        String      numero3         =   (sms.getString(sms.getColumnIndex("sms3")));
        String      numero4         =   (sms.getString(sms.getColumnIndex("sms4")));
        String      numero5         =   (sms.getString(sms.getColumnIndex("sms5")));
        String      msg             =   "Alarme perte de verticalité, ";        
        sms.close();

            if(!numero1.equals("aucun")){
                SmsManager.getDefault().sendTextMessage(numero1, null, msg+" "+getLog(context), null, null);
            }           
            if(!numero2.equals("aucun")){
                SmsManager.getDefault().sendTextMessage(numero2, null, msg/*+" "+getLog(context)*/, null, null);
            }           
            if(!numero3.equals("aucun")){
                SmsManager.getDefault().sendTextMessage(numero3, null, msg/*+" "+getLog(context)*/, null, null);
            }           
            if(!numero4.equals("aucun")){
                SmsManager.getDefault().sendTextMessage(numero4, null, msg/*+" "+getLog(context)*/, null, null);
            }
            if(!numero5.equals("aucun")){
                SmsManager.getDefault().sendTextMessage(numero5, null, msg/*+" "+getLog(context)*/, null, null);
            }
        }

private void call(Context context){

    DBAdapter   smsBDD          =   new DBAdapter(context);     
    Cursor      phone           =   smsBDD.selectCascade();

    String      numero1         =   (phone.getString(phone.getColumnIndex("cascade1")));
    /*String    numero2         =   (phone.getString(phone.getColumnIndex("cascade2")));
    String      numero3         =   (phone.getString(phone.getColumnIndex("cascade3")));
    String      numero4         =   (phone.getString(phone.getColumnIndex("cascade4")));
    String      numero5         =   (phone.getString(phone.getColumnIndex("cascade5")));*/
    phone.close();

        if(!numero1.equals("aucun")){
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + numero1));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
            startActivity(intent);      
        }           

Solution

  • Well, nevermind, just found out it was caused by the FLAG_ACTIVITY_NEW_TASK, since this service was started by another one !

    Problem solved !