Andhika Vnix
Andhika Vnix

Reputation: 11

Create Several Pending Intent AlarmManager And Cancel specific Alarm

I'm trying to create several scheduled alarm notifications by using pending intent and set a trigger button to cancel the alarm. i've successfully create an alarm and cancel it however it seems i cant create multiple alarm before the current alarm is deleted. And from there i need to cancel specific alarm.

Here's my code : TestActivity.java

    public class TestActivity extends AppCompatActivity {
    Button btnStart, btnCancel;
    AlarmManager alarmManager;
    Intent intent;
    PendingIntent pendingIntent;

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

        btnStart = (Button) findViewById(R.id.btnStart);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StartAlarm();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CancelAlarm();
            }
        });
    }

    void StartAlarm(){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 5);
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

        GlobalVariable.Alarm_ID += 1;

        intent = new Intent(this, AlarmReceiver.class);

        pendingIntent = PendingIntent.getBroadcast(this, GlobalVariable.Alarm_ID, intent, PendingIntent.FLAG_IMMUTABLE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
            Toast.makeText(this, "Alarm Set To : "  + cal.getTimeInMillis(), Toast.LENGTH_SHORT).show();
        }
    }

    void CancelAlarm(){
        intent = new Intent(this, AlarmReceiver.class);

        alarmManager.cancel(pendingIntent);
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
    }
}

AlarmReceiver.java

    public class AlarmReceiver extends BroadcastReceiver {
    Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    @Override
    public void onReceive(Context context, Intent intent) {
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());

        Intent notificationIntent = new Intent(context, InfoActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, "Channel ID")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Order Service")
                .setContentText("Alarm_ID : " + GlobalVariable.Alarm_ID + ". Time : " + timeStamp)
                .setGroup("Orders")
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        GlobalVariable.Alarm_ID = 1;

        notificationManager.notify(GlobalVariable.Alarm_ID, notifBuilder.build());
    }
}

There's no error in the program, i just don't know where to change to create several alarm and cancel some of it.

TIA

Upvotes: 0

Views: 215

Answers (1)

Prat
Prat

Reputation: 142

Method to cancel notification by Id:

    public static void cancelNotification(Context ctx, int notifyId) {
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
      nMgr.cancel(notifyId);
    }

Upvotes: 0

Related Questions