tiredeyes
tiredeyes

Reputation: 21

Android cancel alarmmanager

I am trying to test alarmmanager and I can get it to work. Via BroadcastReceiver, i start a service to create a notification. I managed to do this with the help of materials that i found here as well. The problem that i am facing now and had me stuck for a day is cancelling the repeating alarm that i created. It only said that it have to match the pendingintent that was used to create it and i also hardcoded the id's to make sure they match. To an untrained eye like mine i don't see a difference to the examples that i've read here but i am hoping that you could help me find what i need to change.

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


import java.util.Calendar;

public class CreateNotificationActivity extends Activity {

    private PendingIntent pendingIntent;
    private static AlarmManager alarmManager;
    private Intent myIntent;

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

        if(alarmManager == null){
            alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        }

        myIntent = new Intent(this , Receiver.class);

    }

    public void createNotification(View view) {

        pendingIntent = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent);
    }

    public void cancelNotification(View view) {

        pendingIntent = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();
    }
}

how do i cancel the alarm that i created? and is there a way to cancel all alarms without the id's?

Upvotes: 1

Views: 736

Answers (1)

tiredeyes
tiredeyes

Reputation: 21

This solved my problem.

public class Alarm extends BroadcastReceiver

{

Calendar calendar;
String title;
String message;

@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager mNM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(context.getApplicationContext(), TodayScreenActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0);

    String t = intent.getStringExtra("title");
    String m = intent.getStringExtra("message");

    Notification mNotify = new Notification.Builder(context)
            .setContentTitle(t)
            .setContentText(m)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setContentIntent(pIntent)
            .setSound(sound)
            .addAction(0, "Load Workbook", pIntent)
            .build();



    mNM.notify(intent.getIntExtra("id", 1), mNotify);

    wl.release();
}

public void SetRepeatAlarm(Context context, Calendar c, int interval, int id)
{
    AlarmManager am=(AlarmManager)context.getSystemService( Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    i.putExtra("title", title);
    i.putExtra("message", message);
    i.putExtra("id", id);
    PendingIntent pi = PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pi); //1000 * 60 * 60 * 24 Millisec * Second * Minute * hours
    Log.d("Dell", "Alarm - SetRepeatAlarm");
}

public void CancelAlarm(Context context, Task task)
{
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, (int)task.getTASK_COL_ID(), intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
    Log.d("Dell", "Alarm - CancelAlarm");
}

}

Upvotes: 1

Related Questions