Service to create a notification every day at a specific time


I want my app to show a notification with a "GOOD MORNING" message at 6 A.M everyday. As I read, for this I need the app to run in background so I need to use Service.

I have tried the following code but I'm stuck.

public void onClickStartService(View v)
        startService(new Intent(this,MyService.class));

    public void onClickStopService(View v)
        stopService(new Intent(this,MyService.class));

and is

public class MyService extends Service{

    private static final String TAG = "MyService";

    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;

    public void onCreate() {
        Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

    public void onStart(Intent intent, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    //Note: You can start a new thread and use it for long background processing from here.

    public void onDestroy() {
        Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");


I have buttons to start and stop the Service and it works. Now I want the service to create notification as I have mentioned at the beginning of the post. How can I do this?

To start the service at a specific time, I suggest you create a BroadcastReceiver triggered by an Alarm, which will in turn start your service.

First write a BroadcastReceiver like this :

public class AlarmReceiver extends BroadcastReceiver {

    public void onReceive(final Context context, final Intent intent) {
        context.startService(new Intent(context, MyService.class));

     * Schedule the next update
     * @param context
     *            the current application context
    private static void scheduleServiceUpdates(final Context context) {
        // create intent for our alarm receiver (or update it if it exists)
        final Intent intent = new Intent(context, AlarmReceiver.class);
        final PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // compute first call time 1 minute from now
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 10);
        long trigger = calendar.getTimeInMillis();

        // set delay between each call : 24 Hours
        long delay = 24 * 60 * 60 * 1000;

        // Set alarm
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, trigger, delay, pending);
        // you can use RTC_WAKEUP instead of RTC to wake up the device

Then you just need to call the scheduleServiceUpdate method to start the reccuring event. If you only use the RTC type, then if the phone is locked when the alarm should trigger the service, it won't and will wait until the device is unlocked by the user. If you use RTC_Wakeup, the service will start exactly at the time given.

Note that there are other methods in the AlarmManager to trigger events.