-1
snoozebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class);
            myIntent.putExtra("alarm", this);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);

            AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);

            alarmManager.set(AlarmManager.RTC_WAKEUP, getAlarmTime().getTimeInMillis(), pendingIntent); 

            Toast.makeText(getApplicationContext(), "Snoozed for 10 mins", Toast.LENGTH_LONG);
            finish();
        }

    });

when i click snooze button, alarm must restart after 10 mins but , this is not working.

enter image description here

i set alarm after 10 seconds, this is the logcat which is after 10sec when i click snooze button

Full LOGCAT

enter code here

enter image description here

enter image description here

public class AlarmAlertActivity extends Activity implements OnClickListener {

private Calendar alarmTime = Calendar.getInstance();

private Alarm alarm;
private MediaPlayer mediaPlayer;
private StringBuilder answerBuilder = new StringBuilder();
private Vibrator vibrator;
private boolean alarmActive;
private TextView problemView;
private TextView answerView;
private TextView msg_txt, alarmname_txt;
private ImageView img1;
        Spinner spinner_snooze;
private String answerString;
        int hr1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.simple);

    Bundle bundle = this.getIntent().getExtras();
    alarm = (Alarm) bundle.getSerializable("alarm");
    Button can1 = (Button) findViewById(R.id.can1);
    Button snoozebtn = (Button) findViewById(R.id.snoozebtn);

    Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
    Animation shakeimage = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shakeimage);
    msg_txt = (TextView) findViewById(R.id.msg_txt);
    alarmname_txt = (TextView) findViewById(R.id.alarmname_txt);
    img1 = (ImageView) findViewById(R.id.img1);
    img1.setImageResource(R.drawable.smileyblack);


    Calendar c = Calendar.getInstance(); // Get current time
    hr1 = c.get(Calendar.HOUR_OF_DAY); // Gets the current hour of the day ( from 1 to 24 )

    startAlarm();

    if (hr1 < 12) {
            findViewById(R.id.img1).startAnimation(shakeimage);

            msg_txt.setText("Good morning!");
            alarmname_txt.setText(alarm.getAlarmName());

        } else if(hr1 > 12 && hr1 < 17) {
            findViewById(R.id.msg_txt).startAnimation(shake);
            findViewById(R.id.img1).startAnimation(shakeimage);
            msg_txt.setText("Good afternoon!");
            alarmname_txt.setText(alarm.getAlarmName());

        } else if(hr1 > 17 && hr1 < 20) {

            findViewById(R.id.img1).startAnimation(shakeimage);
            msg_txt.setText("Good evening!");
            alarmname_txt.setText(alarm.getAlarmName());

        } else {
            findViewById(R.id.msg_txt).startAnimation(shake);
            msg_txt.setText("Good night!");
            alarmname_txt.setText(alarm.getAlarmName());

        }


    can1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();

        }
    });

    snoozebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Calendar c1 = Calendar.getInstance(); // Get current time
            long getTimeaftertenminute = c1.getTimeInMillis() + 5000;

            Intent myIntent = new Intent(getApplicationContext(), AlarmAlertBroadcastReciever.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),  1, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, getTimeaftertenminute,pendingIntent );

            Toast.makeText(getApplicationContext(), "Snoozed for 30 sec ", Toast.LENGTH_LONG);

            finish();
        }

    });

}

@Override
protected void onResume() {
    super.onResume();
    alarmActive = true;
}

private void startAlarm() {
    if (alarm.getAlarmTonePath() != "") {
        mediaPlayer = new MediaPlayer();
        if (alarm.getVibrate()) {
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            long[] pattern = { 1000, 200, 200, 200 };
            vibrator.vibrate(pattern, 0);
        }
        try {
            mediaPlayer.setVolume(1.0f, 1.0f);
            mediaPlayer.setDataSource(this,
                    Uri.parse(alarm.getAlarmTonePath()));
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (Exception e) {
            mediaPlayer.release();
            alarmActive = false;
        }
    }

}

@Override
public void onBackPressed() {
    if (!alarmActive)
        super.onBackPressed();
}

@Override
protected void onDestroy() {
    try {
        if (vibrator != null)
            vibrator.cancel();
    } catch (Exception e) {

    }
    try {
        mediaPlayer.stop();
    } catch (Exception e) {

    }
    try {
        mediaPlayer.release();
    } catch (Exception e) {

    }
    super.onDestroy();
}

} }

This is alarm activity

Anwesh
  • 91
  • 1
  • 1
  • 12

1 Answers1

0

It is very easy to snooze the alarm just set the the alarm with the same id you do not need to cancel the previous alarm following is the example.

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), sameid, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, getTimeaftertenminute,pendingIntent );

Please edit the sameid variable in sample code with same number that given at the time of setting alarm.This will cancel the alarm and will set the new one.

As your logcat suggest there must be some object which you are trying to access is null inside onCreate method of AlarmAlertActivity.

Please paste your AlarmAlertActivity onCreate method.

Hope it will help :).

Nitin
  • 1,966
  • 4
  • 22
  • 53