Wednesday, November 20, 2013

[android-developers] Sending a SMS with BroadcastReceiver

Hello everyone, 

I have the problem, that I can send a SMS within my BroadcastReceiver - class. However, it is always the same message that will be sent.

The app has some buttons and one numberpicker.

As soon as the first number is being picked a second class ( BroadcastReceiver - class ) is being triggered. 
No, if the user wants to send a new message, he or she picks another number the same message will be sent and I do not know why?!

I have already tried to remove the key in the intent but nothing helps me.

Maybe, someone could help me, please?

Thank you very much!

BR,
Bernd

Activity class:

public class AlarmManagerActivity extends Activity {

private AlarmManagerBroadcastReceiver alarm;
Button button1, button2, button3, button4, button5;
NumberPicker np;
TimePicker tp;
int hour, minute;
Data data = new Data();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_manager);
        alarm = new AlarmManagerBroadcastReceiver();
        tp = new TimePicker(getBaseContext());
        addListenerOnButton();
        tp = (TimePicker)findViewById(R.id.timePicker1);
        tp.clearFocus();
        np = (NumberPicker) findViewById(R.id.numberPicker1);
        np.setMinValue(0);
        np.setMaxValue(1440);
        np.setWrapSelectorWheel(true);
        np.setValue(1);
        data.setTimer(np.getValue());
    }
    
    public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ( np.getValue() > 0 )
Toast.makeText(getBaseContext(), "15 Minuten mit " + np.getValue() + " Minuten Intervall sind gebucht", Toast.LENGTH_SHORT).show();
else if (np.getValue() == 0)
Toast.makeText(getBaseContext(), "15 Minuten sind gebucht", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "15 Minuten mit " + np.getValue() + "ner Minute Intervall sind gebucht", Toast.LENGTH_SHORT).show();       

int a = np.getValue();
data.setTimer(a);
alarm.SetAlarm(getApplicationContext(), np.getValue(), tp.getCurrentHour(), tp.getCurrentMinute());
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ( np.getValue() > 0 )
Toast.makeText(getBaseContext(), "30 Minuten mit " + np.getValue() + " Minuten Intervall sind gebucht", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "30 Minuten sind gebucht", Toast.LENGTH_SHORT).show();
alarm.SetAlarm(getApplicationContext(), np.getValue(), tp.getCurrentHour(), tp.getCurrentMinute());
button1.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ( np.getValue() > 0 )
Toast.makeText(getBaseContext(), "60 Minuten mit " + np.getValue() + " Minuten Intervall sind gebucht", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "60 Minuten sind gebucht", Toast.LENGTH_SHORT).show();
alarm.SetAlarm(getApplicationContext(), np.getValue(), tp.getCurrentHour(), tp.getCurrentMinute());
button1.setEnabled(false);
button2.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
}
});
button4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ( np.getValue() > 0 )
Toast.makeText(getBaseContext(), "90 Minuten mit " + np.getValue() + " Minuten Intervall sind gebucht", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "90 Minuten sind gebucht", Toast.LENGTH_SHORT).show();
alarm.SetAlarm(getApplicationContext(), np.getValue(), tp.getCurrentHour(), tp.getCurrentMinute());
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button5.setEnabled(false);
}
});
button5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if ( np.getValue() > 0 )
Toast.makeText(getBaseContext(), "120 Minuten mit " + np.getValue() + " Minuten Intervall sind gebucht", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "120 Minuten sind gebucht", Toast.LENGTH_SHORT).show();
alarm.SetAlarm(getApplicationContext(), np.getValue(), tp.getCurrentHour(), tp.getCurrentMinute());
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
}
});
}
    
    @Override
protected void onStart() {
super.onStart();
}

    public void startRepeatingTimer(View view) {
    Context context = this.getApplicationContext();
    if(alarm != null){
    alarm.CancelAlarm(context);
    button1.setEnabled(true);
    button2.setEnabled(true);
    button3.setEnabled(true);
    button4.setEnabled(true);
    button5.setEnabled(true);
    Toast.makeText(context, "Wiederholung wurde gestoppt!", Toast.LENGTH_SHORT).show();
    //alarm.SetAlarm(context);
    }else{
    Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
    }
/*    
    public void cancelRepeatingTimer(View view){
    Context context = this.getApplicationContext();
    if(alarm != null){
    alarm.CancelAlarm(context);
    }else{
    Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
    }
    */
/*    
    public void onetimeTimer(View view){
    Context context = this.getApplicationContext();
    if(alarm != null){
    alarm.setOnetimeTimer(context);
    }else{
    Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
    }
*/    
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_widget_alarm_manager, menu);
        return true;
    }
}


BroadcastReceiver class:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

final public static String ONE_TIME = "onetime";
Data data = new Data();
    int v = data.getTimer();

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
         //Acquire the lock
         wl.acquire();

         SmsManager smsManager = SmsManager.getDefault();
         //smsManager.sendTextMessage("082820200", null, "test", null, null);
        smsManager.sendTextMessage("xxx", null, extras.get(ONE_TIME).toString() , null, null);
         
         //You can do the processing here update the widget/remote views.
         
         //StringBuilder msgStr = new StringBuilder();

         
        /* if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
        msgStr.append("One time Timer : ");
         }
         Format formatter = new SimpleDateFormat("hh:mm:ss a");
         msgStr.append(formatter.format(new Date()));

         Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
         */
         //Release the lock
         wl.release();
}
public void SetAlarm(Context context, long timer, int hour, int minute){
if(timer != 0){//set repeating timer
       AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
       Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        intent.removeExtra(ONE_TIME);
        intent.removeExtra(SECOND);
       intent.putExtra(ONE_TIME, "a");
       PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
       //start in 2 seconds
       /*Calendar calendar = Calendar.getInstance();
       calendar.setTimeInMillis(System.currentTimeMillis());
       calendar.add(Calendar.SECOND, 2);*/
       //when to start
       Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, hour);
timeOff9.set(Calendar.MINUTE, minute);
//timeOff9.set(Calendar.SECOND, 00);
//when to start
       am.setRepeating(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), timer*60000, pi);
       }else{//one timer
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
           //Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        intent.removeExtra(ONE_TIME);
        intent.removeExtra(SECOND);
           intent.putExtra(SECOND, "b");
           PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
           am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
    }
    public void CancelAlarm(Context context)
    {
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
    /*
    public void setOnetimeTimer(Context context){
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
        intent.putExtra(ONE_TIME, Boolean.TRUE);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
    }
    */
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate