user2648852
user2648852

Reputation: 711

Service to create a notification everyday at a specified 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.

MainActivity.java

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

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

and MyService.java is

public class MyService extends Service{

    private static final String TAG = "MyService";

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

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

    @Override
    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.
    }

    @Override
    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?

Upvotes: 2

Views: 3688

Answers (2)

kord
kord

Reputation: 479

You can start from PendingIntent and AlarmManager

Tutorial here

Dont forget to add possibility to cancel alarm manager with

mAlarmManager.cancel(pendingIntent);

Also you may want to intercept android.intent.action.BOOT_COMPLETED event to make you app starting immediately after reboot if you want to start your service by schedule.

Upvotes: 3

XGouchet
XGouchet

Reputation: 10203

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 {


    @Override
    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.

Upvotes: 4

Related Questions