4

I am trying to figure out how I should launch a notification. Creating the notification is not what I am asking, but rather a way to launch it in the background so its unobtrusive and the user can do whatever they were doing. Its for a calendar, a reminder to be exact. It is also important to note I am using AlarmManager.

  1. What method should I use to run it in the background. BroadCastReciever, Service, etc.

  2. Research I have found also presents a problem with AlarmManager. When the app is killed or phone is turned off, the alarm is also. What other method should I use in order to make sure the notification is guaranteed to show for that event reminder?

If any additional info is needed please ask and I shall do so. Thanks in advance.

Andy
  • 10,553
  • 21
  • 75
  • 125

3 Answers3

3

Create a broadcastreceiver or intentservice. Then...

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


Date date = new Date(); //set this to some specific time
or Calendar calendar = Calendar.getInstance();

//set either of these to the correct date and time. 

then 
Intent intent = new Intent();
//set this to intent to your IntentService or BroadcastReceiver
//then...
PendingIntent alarmSender = PendingIntent.getService(context, requestCode, intent,
                            PendingIntent.FLAG_CANCEL_CURRENT);
//or use PendingIntent.getBroadcast if you're gonna use a broadcast

                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender); // date.getTime to get millis if using Date directly. 

If you'd like these alarms to work correctly even when the phone is restarted, then add:

        <action android:name="android.intent.action.BOOT_COMPLETED"/>

as intentfilter on your Receiver in the manifest and recreate your alarms in onReceive.

EDIT

When you create a BroadcastReceiver in your application, it allows to do exactly what it sounds like: receive broadcasts in the system. So for example, you might some BroadcastReceiver like so:

public class MyAwesomeBroadcastReceiver extends BroadcastReceiver {

//since BroadcastReceiver is an abstract class, you must override the following:

    public void onReceive(Context context, Intent intent) {
       //this method gets called when this class receives a broadcast
    }
}

To send broadcasts to this class explicitly, you define the receiver inside of the manifest, as follows:

<receiver android:name="com.foo.bar.MyAwesomeBroadcastReceiver" android:enabled="true" android:exported="false">
            <intent-filter>

                <action android:name="SOME_AWESOME_TRIGGER_WORD"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>




            </intent-filter>
        </receiver>

Having this in the manifest gets you two things: You can send a broadcast explicitly to your receiver whenever you want by

Intent i = new Intent("SOME_AWESOME_TRIGGER_WORD");
                sendBroadcast(intent);

Also, since you've told android you'd like to receive the BOOT_COMPLETED action which is broadcast by the system, your receiver will also get called when that happens.

LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
  • Hmm, I will take another look at `BroadcastReciever` and I really appreciate the xml. Gives me something to research on that front. Would it be wrong to just create an Activity with no UI and have it create the notification? – Andy Jul 06 '12 at 03:14
  • Actually, I am trying to figure out what the last part does, or how it works really. I am still relatively new to android, so I am still learning all the stuff it can do. Adding that to where in the manifest exactly? Where is the inReceive? – Andy Jul 06 '12 at 03:23
  • AHHH! Wow I really appreciate your time. That made complete sense. So there is just one thing I am a little hazy on. When calling `AlarmManager`, you give it a `PendingIntent`, which it calls when the time the alarm was set to go off. But if you do what you did: `sendBroadcast(intent)`, wouldn't that call it immediately? Like where do you use `sendBroadcast()`? Hopefully what I am asking makes sense. – Andy Jul 06 '12 at 19:16
  • I just noticed you added in a comment `PendingIntent.getBroadcast()`. What does it do exactly. The Doc says it returns a `PendingIntent`, but how do I use it with `AlarmManager`? Do I not send the `PendingIntent` with the `AlarmManager`? Sorry if I am asking alot. I just like understanding. – Andy Jul 06 '12 at 19:24
2

Use AlarmManager is the best practice.

Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
  • Well thats good to know. How can I solve that problem though, with it canceling if the app is killed or phone is shut off? – Andy Jul 06 '12 at 02:58
  • The app is killed. The AlarmManager will work correctly. when the phone is shut off. No app can startup including the Clock.. when the phone restart again. receive the STARTUP broadcast and regist the Alarm again. – Changwei Yao Jul 06 '12 at 03:06
  • When the device boot completed, a broadcast will be deliveried. the action is android.intent.action.BOOT_COMPLETED, you need to declare the permission android.permission.RECEIVE_BOOT_COMPLETED – Changwei Yao Jul 06 '12 at 03:37
1

Here is what you can do :

  1. Launch Service through your AlarmManager's pending intent and write your Notification code in that service.

  2. Use a database to store all your Alarms and then reschedule them on device restart by using BOOT_COMPLETED Broadcast reciver.

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
  • Yes, that is definitely one way to do it. I do have one question though. With a `Service`, I must call it, I cannot just add the class name when instantiating the new `Intent`... unless I can. Like I wanted to use a `Service`, but I noticed that `startService()` needed to be called, so that wouldn't work. That means I need 2 classes in the background running. One that gets called by `AlarmManager` and the `Service` that gets called by the class `AlarmManager` just called. How should the class be that calls the `Service`? – Andy Jul 06 '12 at 19:26
  • Sorry, I didn't quite catch that. Can you please explain it in a simpler way? – Vipul Purohit Jul 07 '12 at 06:30