0

I am currently working on an alarm to be included in a bigger project. Everything is working correctly with the datepicker/timepicker. And the alarm does start once the date and time has been reached. The problem i am having is the only way to stop the alarm is by closing the application. I have added a button (stopAlarm) with everything i thought would be needed to stop this pending intent, but it is still not stopping the alarm?

I am probably overlooking something very small but i was wondering if there is anyone who can point me in the right direction?

Thanks!

MainActivity:

package servicealarmdemo.test2;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity{

DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
Button buttonStopAlarm;
TextView info;

final static int RQS_1 = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    info = (TextView)findViewById(R.id.info);
    pickerDate = (DatePicker)findViewById(R.id.pickerdate);
    pickerTime = (TimePicker)findViewById(R.id.pickertime);

    Calendar now = Calendar.getInstance();

    pickerDate.init(
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH),
            null);

    pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
    pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));

    buttonSetAlarm = (Button)findViewById(R.id.setalarm);
    buttonSetAlarm.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            Calendar current = Calendar.getInstance();

            Calendar cal = Calendar.getInstance();
            cal.set(pickerDate.getYear(),
                    pickerDate.getMonth(),
                    pickerDate.getDayOfMonth(),
                    pickerTime.getCurrentHour(),
                    pickerTime.getCurrentMinute(),
                    00);

            if(cal.compareTo(current) <= 0){
                //The set Date/Time already passed
                Toast.makeText(getApplicationContext(),
                        "Date/Time Has Already Passed!",
                        Toast.LENGTH_LONG).show();
            }else {
                setAlarm(cal);
            }

            buttonStopAlarm = (Button)findViewById(R.id.stopalarm);
            buttonStopAlarm.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                    alarmManager.cancel(pendingIntent);

        }});

}


private void setAlarm(Calendar targetCal){

    info.setText("\n\n***\n"
            + "Deadline has now been set: " + targetCal.getTime() + "\n"
            + "***\n");

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}

}

AlarmReceiver.java:

package servicealarmdemo.test2;

import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;

public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
    ringtone.play();
}
}

XML:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <DatePicker
            android:id="@+id/pickerdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TimePicker
            android:id="@+id/pickertime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/setalarm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Set Alarm"/>
        <Button
            android:id="@+id/stopalarm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="stopAlarm"
            android:text="Stop Alarm"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</ScrollView>

ZenoX
  • 117
  • 9

1 Answers1

1

You have a different mechanism to connect your methods with the view - setAlarm is with code (onClickListener) and stopAlarm is with XML. It could be a problem with the way the XML is connecting to the view/Activity, or a problem in the stopAlarm method itself. Put some debugging in the stopAlarm method to confirm it is getting called.

Also, according to this post, you may need to use the "CURRENT_UPDATE_FLAG" when getting the pending intent.

public void stopAlarm(View view) {
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);

}

One other thing to try is caching the intent between set and stop alarm. Rather than re-creating the intent.

Community
  • 1
  • 1
hack_on
  • 2,532
  • 4
  • 26
  • 30
  • Thanks for the reply, i changed the stopAlarm method to match setAlarm using an (onClickListener). Also updated the code using FLAG_UPDATE_CURRENT but nothing happens when i press the stop alarm button. The alarm continues to fire. – ZenoX Feb 20 '17 at 23:01
  • @ZenoX Did you put some debugging? is stopAlarm getting called? – hack_on Feb 20 '17 at 23:03
  • 1
    @ZenoX try keeping a reference to the intent and re-using it instead of creating a new intent. There could be something different about it even though I can't see anything. – hack_on Feb 20 '17 at 23:12
  • Thanks for the help, i will continue to look into it. I am sure i am missing something, i just can't figure out what. – ZenoX Feb 20 '17 at 23:15