2

i have come across multiple solutions on how i can cancel a pending intent however have come to no avail. I have also been unable to trigger the cancellation in which the codes i have placed in an on-click function. The button triggering the function is code generated. The cancellation was unable to be fired and it continues into the intended receiver and here are my codes :

package com.nyp.contactmanagement_2307;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class Alarmfinal extends Activity {

DatePicker pickerDate;
TimePicker pickerTime;
Button buttonSetAlarm;
TextView info;
int count = 0;  
LinearLayout savedAlarm;
EditText notes;
final static int RQS_1 = 1;
String notes1;
String am;
int realhrs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarmfinal);

    info = (TextView)findViewById(R.id.info);
    notes = (EditText)findViewById(R.id.notes);
    savedAlarm = (LinearLayout)findViewById(R.id.savedAlarm);
    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) {

            // getting calender
            if(count < 3){
            Calendar current = Calendar.getInstance();

            Calendar cal = Calendar.getInstance();
            int yr = 2015;
            int mth = 11;
            int day = 01;
            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(), 
                        "Invalid Date/Time", 
                        Toast.LENGTH_LONG).show();
            }else{
                setAlarm(cal, count);
                // crafting the time and date 

                 if(pickerTime.getCurrentHour() == 0){
                    realhrs = 12;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 1){
                    realhrs = 1;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 2){
                    realhrs = 2;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 3){
                    realhrs = 3;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 4){
                    realhrs = 4;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 5){
                    realhrs = 5;
                    am = "am";
                }else if(pickerTime.getCurrentHour() == 6){
                    realhrs = 6;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 7){
                    realhrs = 7;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 8){
                    realhrs = 8;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 9){
                    realhrs = 9;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 10){
                    realhrs = 10;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 11){
                    realhrs = 11;
                    am = "am";

                }else if(pickerTime.getCurrentHour() == 12){
                    realhrs = 12;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 13){
                        realhrs = 1;
                        am = "pm";

                }else if(pickerTime.getCurrentHour() == 14){
                    realhrs = 2;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 15){
                    realhrs = 3;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 16){
                    realhrs = 4;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 17){
                    realhrs = 5;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 18){
                    realhrs = 6;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 19){
                    realhrs = 7;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 20){
                    realhrs = 8;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 21){
                    realhrs = 9;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 22){
                    realhrs = 10;
                    am = "pm";

                }else if(pickerTime.getCurrentHour() == 23){
                    realhrs = 11;
                    am = "pm";

                }


                    // generate fields 

                    // container 
                     final LinearLayout layout = new LinearLayout(Alarmfinal.this);
                     layout.setOrientation(LinearLayout.VERTICAL);
                     layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                     // notes 
                     TextView titleView = new TextView(Alarmfinal.this);
                     titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                     // linaer 2 
                     LinearLayout layout2 = new LinearLayout(Alarmfinal.this);
                     layout2.setOrientation(LinearLayout.HORIZONTAL);
                     layout2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                     // time 
                     TextView tvtime = new TextView(Alarmfinal.this);
                     tvtime.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   

                    // button to cancel alarm
                     Button onoff =  new Button(Alarmfinal.this);
                     onoff.setText("cancel");
                     onoff.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
                     onoff.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            savedAlarm.removeViewAt(v.getId());
                            /*AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
                            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), count, i,PendingIntent.FLAG_UPDATE_CURRENT);

                            manager.cancel(pendingIntent);
                            pendingIntent.cancel();*/


                            PendingIntent.getBroadcast(getBaseContext(),count, getIntent(), 0).cancel();

                            count--;
                        }


                     });

                     layout2.addView(tvtime);
                     layout2.addView(onoff);

                     TextView tvdate = new TextView(Alarmfinal.this);
                     tvdate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));   
                     tvdate.setText("date :" +"/" + "/");
                     tvtime.setText("time : " + realhrs + " : " + pickerTime.getCurrentMinute() + am);
                     notes1 = notes.getText().toString();
                     titleView.setText("notes : "+ notes1);


                     // layout id 
                     layout.setId(count);
                     onoff.setId(count);
                     layout.addView(titleView);
                     layout.addView(layout2);
                     layout.addView(tvdate);

                     savedAlarm.addView(layout);


                        count++;


            }}else{

                Toast.makeText(getBaseContext(), "alarm finish", Toast.LENGTH_SHORT).show();
            }



        }});
} 

private void setAlarm(Calendar targetCal, int count){

    info.setText("\n\n***\n"
            + "Alarm is set@ " + targetCal.getTime() + "\n"
            + "***\n");




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

}

the areas to look at are the cancellation codes in the onoff button on click and in the set alarm method

Cosq
  • 155
  • 1
  • 2
  • 12

1 Answers1

0

Well, the code you have in there now for sure won't work:

PendingIntent.getBroadcast(getBaseContext(),count, getIntent(), 0).cancel();

Using getIntent() here is using the Intent that your Activity was started with.


If you set the alarm using this PendingIntent:

PendingIntent pendingIntent = 
    PendingIntent.getBroadcast(getBaseContext(),count, intent, 0);

You should cancel it using this:

Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = 
    PendingIntent.getBroadcast(getBaseContext(), count, i, 0);

manager.cancel(pendingIntent);
pendingIntent.cancel();

If this isn't working for you, then probably the value of count isn't the same. The value of count must be identical in both calls.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • oh i tried making this but for the manager, if i create a local variable Alarmmanager manager on inside of the onclick, does it refer to the same one i use to set? – Cosq Nov 10 '15 at 15:59
  • There is only ever one `AlarmManager`. You can either store a reference to the `AlarmManager` in a field (member variable), or you can call `getSystemService(Context.ALARM_SERVICE)` to get it every time. – David Wasser Nov 10 '15 at 16:09
  • thank man your answer really works best and have contributed to my mates (: keep it up man! cheers: i do have another post regarding a content provider would really appreciate it if u do look it up if u have a chance – Cosq Nov 14 '15 at 14:19