• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Action buttons on Broadcast receiver

D

Deleted User

Guest
Is it posible to make an action button that will cause notification to reappear after one hour. Then I want to make another action button that will cancel notification and make it reappear the next day again. Any ideas for how can I do this? Here is the code -> Main Activity:

  1. EditText write;
  2. Button notification;

  3. @override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);

  7. notification = findViewById(R.id.notification);
  8. write = findViewById(R.id.write);

  9. notification.setOnClickListener(new View.OnClickListener() {
  10. @override
  11. public void onClick(View v) {



  12. Calendar calendar = Calendar.getInstance();
  13. calendar.setTimeInMillis(System.currentTimeMillis());
  14. calendar.set(Calendar.HOUR_OF_DAY, 12);
  15. calendar.set(Calendar.MINUTE, 57);
  16. calendar.set(Calendar.SECOND, 0);
  17. Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
  18. intent.putExtra("TEXT", String.valueOf(write.getText()).trim());

  19. PendingIntent pendingIntent =PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

  20. AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
  21. alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);


  22. }
  23. });



  24. }
And here the Broadcast Receiver:

  1. public class Notification_receiver extends BroadcastReceiver {
  2. @override
  3. public void onReceive(Context context, Intent intent) {

  4. String text = intent.getStringExtra("TEXT");

  5. NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

  6. Intent repeating_intent = new Intent(context, MainActivity.class);
  7. repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  8. repeating_intent.putExtra("action","snooze");
  9. repeating_intent.putExtra("action","cancel");
  10. PendingIntent pendingIntent =PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);

  11. Intent snoozeintent = new Intent(context, ActionBtnReceiver.class);
  12. snoozeintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  13. snoozeintent.putExtra("action","snooze");
  14. PendingIntent snzpendingIntent = PendingIntent.getActivity(context, 100, snoozeintent, PendingIntent.FLAG_UPDATE_CURRENT);

  15. Intent cancelintent = new Intent(context, ActionCancelBtnReceiver.class);
  16. cancelintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  17. cancelintent.putExtra("action","cancel");
  18. PendingIntent cnlpendingIntent =PendingIntent.getActivity(context, 100, cancelintent, PendingIntent.FLAG_UPDATE_CURRENT);





  19. NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
  20. .setContentIntent(pendingIntent)
  21. .setSmallIcon(R.drawable.ic_launcher_background)
  22. .setContentTitle("Notification")
  23. .setContentText(text)
  24. .setAutoCancel(true)
  25. .addAction(R.drawable.ic_snooze_black_24dp, "Snooze", snzpendingIntent)
  26. .addAction(R.drawable.ic_cancel_black_24dp, "Cancel", cnlpendingIntent);
  27. notificationManager.notify(100, builder.build());

  28. }
The ActionBtnReceiver and ActionCancelBtnReceiver are empty because I don't know how to do it. Can you please help me? java android broadcastreceiver. I asked again but nobody answered me. So please help me because I can'y find a suitable answer anywhere.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones