This qustion has been asked alot and i tried alot of the answer and didn't work so this is the most common one
public void onClick(View v) {
boolean alarmUp = (PendingIntent.getBroadcast(MainActivity.this ,0,
new Intent(MainActivity.this,Notifications.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (alarmUp)
{
am.cancel(pend);
Intent alarmIntent = new Intent(MainActivity.this,Notifications.class);
final PendingIntent pendingIntent =
PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null) {
pendingIntent.cancel();
}
Toast.makeText(MainActivity.this,"Tweak cleared", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"There is no Tweak!", Toast.LENGTH_LONG).show();
}
this button is in my MainActivity and everytime it's pressed i want to check if there is an alarm or no then if there is cancel the alarm "am" and toast massage..now the app always toasts the massage in the else statement "There is no Tweak!" even if there is alarm..i'm beginner in android so would appreciate any help..thanks.
edit
this is another button in my MainActivity that starts the alarm
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), Notifications.class);
pend = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pend);
i press this button before i press the other one that cancels it.
edit
this is my other attempt to check for alarm and cancel it..it works but when i close the app and open it again it tells that there is not alarm "there is no tweak!" even if there is one
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (am != null && pend != null) {
am.cancel(pend);
pend = null;
Toast.makeText(MainActivity.this,"Tweak cleared", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"There is no Tweak!", Toast.LENGTH_LONG).show();
}
}
});
pend and am are prviate variables which are initialized in the "start" button
Thanks