0

So, I seem to have fallen down a rabbit hole trying to figure out the best way to notify a user of an alarm going off.

Basically, I want some kind of notification/dialog to come up at a certain time, and it should come up no matter what the user is doing, and block further use until acted upon (dismissed or otherwise).

Right now, I have an AlarmManager, and the BroadcastReceiver that is registered with it starts a new service.

Every time I thought I was heading in the right direction, I hit a problem where someone online had a similar issue, and was told "don't do it that way." (Having a service create/show an AlertDialog, for instance.)

I was hoping someone could give me a brief list of what their recommendation would be; I don't need code (at least I shouldn't), just some high level abstraction.

UnsettlingTrend
  • 452
  • 1
  • 5
  • 20

3 Answers3

0

Go with Notification, which plays a sound perhaps, that would pull your user's attention to your notification, just like the default alarm does.

And make the notification an ongoing one. Which can't be removed by the user, until and unless some action is performed to change the state of the notification.

Android: How to create an "Ongoing" notification?

Dialogs for this situation would be annoying for me. The docs also suggest not to use them in these scenarios.

Community
  • 1
  • 1
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • It's an alarm application though; I kind of WANT it to be annoying. I would like something similar to what the stock alarm app does: Pop up a dialog that forces the user to act, and if snoozed--or the something similar--THEN set a notification that can be acted upon later. Is what the default app shows when the alarm first goes off (the menu with snooze/dismiss) NOT a dialog? – UnsettlingTrend Jun 14 '13 at 15:45
0

I did it in this way and it work fine for me.

Create a class and Call it something like ScheduledService it extends IntentService, in this class you'll do what you want to do when alarm goes off.

public class ScheduledService extends IntentService {

public ScheduledService() {
    super("My service");
}

@Override
protected void onHandleIntent(Intent intent) {
    //Do something, fire a notification or whatever you want to do here
    Log.d("debug", "Ring Rind !");

}
}

then in you activity to start the alarm use the following:

AlarmManager mgr = (AlarmManager) YourActivity.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(YourActivity, ScheduledService.class);
PendingIntent pi = PendingIntent.getService(YourActivity, 0, i, 0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);

Which PERIOD is after how much milliseconds you want the alarm to goes off.

To cancel stop the timer and cancel the alarm use:

if (mgr != null)
        mgr.cancel(pi);

Finally for all this to work you need to register your ScheduledService class as a Service. In your manifest add this tou your application:

<application
    ... />
    ...
    <service android:name=".ScheduledService" >
    </service>

</application>

This way the Android OS will take care of firing the alarm when it's time. even if other application is running or even if your app process is terminated.

Hope this help. Regards.

Just a crazy idea: Create an activity and set it's theme to be fullscreen with no title bar and a button to stop the alarm maybe, instead of doing a notification just make an intent that starts that activity "maybe you will need This" to work even when phone is locked and play some annoying sounds, "This" may help, when the activity starts. you can also override the onBackPressed() to do nothing.

Community
  • 1
  • 1
Tarek K. Ajaj
  • 2,461
  • 1
  • 19
  • 24
  • I appreciate that comment, but I already have the alarm scheduling part figured out; I'm wondering on the best way to prompt the user with a dialog/alert when the time comes. – UnsettlingTrend Jun 14 '13 at 15:47
0

Have a look at this Sample Open Source Project

Nargis
  • 4,687
  • 1
  • 28
  • 45