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);
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(); }
source share