How to set an alarm that will disappear even if I restart my Android phone?

I searched this site and found some answers related to setting up an alarm. I managed to set the alarm.

What am I doing:

  • From activity, I set an alarm, which at a certain time and date will call the receiver
  • From the recipient, I call the service
  • From the service, I send a notification to the user (in the notification panel).

My questions:

  • I set the alarm in 5 minutes. Say I turned off the phone and turned it on (it seems he forgot the alarm). How can I prevent this?

  • Do I really need to call the service to send notifications or can I do this from the recipient?

The following is the code mentioned in previous section (a):

Intent intent = new Intent(MyActivity.this, AlarmReceiver.class); intent.putExtra("alarm_message", "Something"); PendingIntent mAlarmSender; mAlarmSender = PendingIntent.getBroadcast( MyActivity.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. long alarmTime = dateMgmt.getTimeForAlarm(pickedDate); // Schedule the alarm! AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000, mAlarmSender); 

This is the code indicated in the previous section (b):

  @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); String message = bundle.getString("alarm_message"); Intent newIntent = new Intent(context, MyService.class); context.startService(newIntent); } catch (Exception e) { Toast .makeText( context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } 

This code, indicated in the previous section (c):

  @Override public void onCreate() { super.onCreate(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); showNotification(); } 
+4
source share
1 answer

You do not need to call the service to send notifications, you can do this from the recipient.

I do not think there is a way to save the alarm after turning off the power. What will i do:

Please note that you will need to save the alarm information somewhere. Check out Android Data-storage .

+2
source

Source: https://habr.com/ru/post/1313413/


All Articles