Reputation: 31
Here's my addAlarm() Method :
Intent intent = new Intent(this, ALReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 10 , intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),pendingIntent );
This code work fine but i want delete this alaram usning following code :
Intent intent = new Intent(this, ALReciver.class);
PendingIntent sender = PendingIntent.getBroadcast(this,10, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
this code is not working.. i am not understand What I am doing wrong?
Upvotes: 1
Views: 207
Reputation: 145
You can cancel alarm. For that you need to create same intent:
alarmMgr.cancel(alarmIntent);
Upvotes: 0
Reputation: 579
This is my sample of start,cancel
I am used same intent. but your mistake is this. pending intent give to common button click time perform action as start and cancel use with that intent then success.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_alarm);
Intent alarmIntent = new Intent(Alaram.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MyActivity.this, 0, alarmIntent, 0);
findViewById(R.id.btn_startAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.btn_stopAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancel();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
It's working fine for me are u try this one.
Upvotes: 1
Reputation: 3009
Replce:
PendingIntent sender = PendingIntent.getBroadcast(this,10, intent,PendingIntent.FLAG_CANCEL_CURRENT);
to
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 10 , intent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
Upvotes: 1
Reputation: 37604
Change the Flag from PendingIntent.FLAG_CANCEL_CURRENT
to PendingIntent.FLAG_UPDATE_CURRENT
.
Upvotes: 0