19

In my main activity which has a button in it. In its onclick listener im calling function to set alarm.The alarm is working but iam not able to stop it .Can some one help me

MainActivity.java

public class MainActivity extends Activity {

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

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setalarm();
            }


        });
    }

    private void setalarm() {

        Calendar cal=Calendar.getInstance();
        // cal.set(Calendar.MONTH,6);
        // cal.set(Calendar.YEAR,2013);
        // cal.set(Calendar.DAY_OF_MONTH,12);
        cal.set(Calendar.HOUR_OF_DAY,18);
        cal.set(Calendar.MINUTE,32);
        Intent intent = new Intent(this, Mote.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent );
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        Toast.makeText(this, "Alarm SET.", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Mote.java:

public class Mote  extends BroadcastReceiver{

    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello you have to take medicine I am Nitin Sharma";
        long when = System.currentTimeMillis();

        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";

        final int NOTIF_ID = 1234;
        NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context, Alset.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context,0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText,when );
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notofManager.notify(NOTIF_ID,notification);

        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

        Intent i = new Intent(context,Alset.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);

Alset.java:

public class Alset extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitystop); 
        Log.e("IM here ","Im here");

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(Alset.this, "Stop the alrm now", Toast.LENGTH_LONG).show();
            }
        });
    }   
}

Once the alarm starts I will get the callback in the receiver's OnReceive() method. From there I'm going to the activity Alset where I kept a stop button. How do I to stop the alarm from here?

NOTE: - I'm hardcoding the time to set the alarm.

Michael
  • 3,093
  • 7
  • 39
  • 83
playmaker420
  • 1,527
  • 4
  • 27
  • 52
  • 1
    you need to cancel alarm from alarm manager try ==== AlarmManager alarmManager = (AlarmManager) _cntx.getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA); alarmManager.cancel(pendingIntent); – user1140237 Jul 12 '13 at 13:42

2 Answers2

37

You can cancel the alarm like this:

     Intent intent = new Intent(this, Mote.class);
     PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
     alarmManager.cancel(pendingIntent);

Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • @droid_dev If you are having trouble please open a new question and post your code and explain what isn't working. The community can help you. – David Wasser Apr 21 '14 at 15:19
  • 1
    alarmManager.cancel(pendingIntent) will just cancel the alarm from the alarmManager. it will not kill the fired activity. i have also raised the question to know how to kill the fired activity. refer http://stackoverflow.com/questions/29735209/android-repeating-alam-is-not-getting-stopperd-on-the-stop-button-clicked pls reply if anyone know the answer – shiv garg Apr 20 '15 at 05:00
  • My initial intent add an action so for this to work I add to add the same action to the intent. – Favolas Dec 22 '15 at 09:40
  • @Favolas yes, of course. You need to create a matching `Intent`, so the ACTION, CATEGORY, DATA and COMPONENT information must all match. – David Wasser Dec 22 '15 at 11:54
  • @DavidWasser Hey mate can you help me with a problem i have been having for 2 - 3 days, here's my question http://stackoverflow.com/questions/43950978/unable-to-set-proper-context-to-cancel-alarmmanager – 4127157 May 13 '17 at 23:29
  • can you please help me to fix this https://stackoverflow.com/questions/51267156/alarm-started-when-opening-the-app-instead-of-on-specific-time-in-android @DavidWasser – Zhu Jul 10 '18 at 14:25
3

you need to cancel alarm from alarm manager try this,

Intent intent = new Intent(this, Mote.class);
AlarmManager alarmManager = (AlarmManager) _cntx.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);          
alarmManager.cancel(pendingIntent);
Charuක
  • 12,953
  • 5
  • 50
  • 88
user1140237
  • 5,015
  • 1
  • 28
  • 56
  • Can you please help me to solve this https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity#51542099 @user1140237 – Zhu Jul 26 '18 at 15:24