Yunus Haznedar Yunus Haznedar - 16 days ago 11
Android Question

Alarm Manager&Broadcast receiver not stopping

I can't stop the alarm manager or broadcast receiver. Any idea to solve this?

Many thanks in advance. By the way I'm using switch button for alarm on/off.

Here is my code;

MainActivity.class

public void setAlarm(Calendar alarmCalender)
{
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalender.getTimeInMillis(), pendingIntent);

}

public void cancelAlarm()
{

AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
aManager.cancel(pIntent);
stopService(intent);
}


Switch button:

bolme1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean aktiflestirmisse)
{
if(aktiflestirmisse)
{
setAlarm(calSet);
btn1.setBackgroundColor(getResources().getColor(R.color.aktifdurum));
Toast t= Toast.makeText(MainActivity.this,"Bölme 1 aktif hale getirildi.",Toast.LENGTH_SHORT);
t.show();
}

else
{
cancelAlarm();
btn1.setBackgroundColor(getResources().getColor(R.color.pasifdurum));
Toast t = Toast.makeText(MainActivity.this, "Bölme 1 pasif hale getirildi.", Toast.LENGTH_SHORT);
t.show();
}


In any case here is my AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
NotificationManager manager;
private final int NOTIFICATION_ID =1;
@Override
public void onReceive(Context context, Intent intent) {

NotificationManager mng=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder nb=new Notification.Builder(context);
nb.setContentTitle("Hatırlatma");
nb.setContentText("İlgili bölmenin vakti ilaç vakti geldi!");
nb.setSmallIcon(R.drawable.alarm_uyari);
nb.setTicker("Bildiriminiz var.");
nb.setAutoCancel(true);
PendingIntent pi=PendingIntent.getActivity(context,NOTIFICATION_ID,intent,0);
nb.setContentIntent(pi);
Notification not=nb.getNotification();
not.vibrate=new long[]{500,500,500,500};
mng.notify(NOTIFICATION_ID,not);

Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}}

Answer

I used this code for my own app:

Create the pending intent and wrap our intent:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//get the alarm manager
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 23)
{
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19)
{
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);
}
else alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), pendingIntent);

Cancel:

private void cancelAlarm(long id)
{
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), (int)id, intent,  PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.cancel(sender);
}

So in your case this will probably work for setting the alarm

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