-1

Alarm Project

In this app, I'm inserting set of values in EditText

a) 3

b) 5554

c) iOS

When you click 'Send Alarm' it will send SMS for every 1 minute. Now i Cancelled Alarm and again I'm starting this app inserting NEW VALUES as follows:

a) 3

b) 12345

c) Android

Starting Alarm again, this time it is not taking NEW VALUES. Instead it is sending sms to '5554' containing text 'iOS'. I want to know how to update the NEW VALUES. I'm having two class. 1) MainActivity 2)Receiver

Code:

public class MyyActivity extends Activity {
EditText et, et1, et2;
Button b, b1;
AlarmManager alarmManager;

String no,text,ala; 
PendingIntent pi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et = (EditText) findViewById(R.id.editTextEnter);
    et1 = (EditText) findViewById(R.id.editTextNUMBER);
    et2 = (EditText) findViewById(R.id.editTextTEXT);
    b = (Button) findViewById(R.id.buttonSendAlarm);
    b1 = (Button) findViewById(R.id.Cancel);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             no = et1.getText().toString();
             text = et2.getText().toString();
             ala = et.getText().toString();


            int foo = Integer.parseInt(ala);
            Intent myIntent = new Intent(MyyActivity.this, MyRecvr.class);
            Bundle bundle = new Bundle();
            bundle.putCharSequence("no", no);
            bundle.putCharSequence("text", text);
            myIntent.putExtras(bundle);
            pi = PendingIntent.getBroadcast(getApplicationContext(), 0,
                    myIntent, 0);

            alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, foo);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*1,pi);

            Toast.makeText(
                    getApplicationContext(),
                    "Start Alarm with \n" + "smsNumber = " + no + "\n"
                            + "smsText = " + text, Toast.LENGTH_SHORT)
                    .show();
            et.setText("");
            et1.setText("");
            et2.setText("");
        }
    });
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getApplicationContext(), MyRecvr.class);
            PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
                           0, intent, 0);
            alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.cancel(sender);
            Toast.makeText(MyyActivity.this, "Cancel!", Toast.LENGTH_LONG)
                    .show();
            finish();

        }
    });
}

And Receiver class:

public class MyRecvr extends BroadcastReceiver {
@SuppressWarnings("deprecation")
SmsManager sms;


@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    String smsNumbr= (String) bundle.getCharSequence("no");
    String smsText = (String) bundle.getCharSequence("text");

    Toast.makeText(context, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
            .show();
    Toast.makeText(
            context,
            "MyAlarmService.onStart() with \n" + "smsNumberToSend = "
                    + smsNumbr + "\n" + "smsTextToSend = "
                    + smsText, Toast.LENGTH_LONG).show();
    sms = SmsManager.getDefault();
    sms.sendTextMessage(smsNumbr, null, smsText, null, null);
}
DroidLearner
  • 2,115
  • 5
  • 31
  • 50

1 Answers1

0

Use FLAG_UPDATE_CURRENT instead of 0 for the last parameter of your getBroadcast() call to create the PendingIntent.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491