Skip to content Skip to sidebar Skip to footer

Show Android Notification Every Five Minutes

I want to know how to set time for notification. I want to set notification every five minutes, so help me do that. public class FirstActivity extends Activity { private static

Solution 1:

I think that the best way will be to create a service that sets the notification and then activate the service using AlarmManager. That's the code for the AlarmManager:

privatevoidstartAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    longwhen = System.currentTimeMillis();         // notification time
    Intent intent = new Intent(this, ReminderService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, when, (AlarmManager.INTERVAL_FIFTEEN_MINUTES / 3), pendingIntent);
}

To control the interval use the constant values or just insert your own value (in milliseconds).

Here is the Service:

publicclassReminderServiceextendsIntentService {
    privatestaticfinalintNOTIF_ID=1;

    publicReminderService(){
        super("ReminderService");
    }

    @OverrideprotectedvoidonHandleIntent(Intent intent) {
        NotificationManagernm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        longwhen= System.currentTimeMillis();         // notification timeNotificationnotification=newNotification(R.drawable.icon, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        IntentnotificationIntent=newIntent(this, YourActivity.class);
        PendingIntentcontentIntent= PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
        nm.notify(NOTIF_ID, notification);
    }

}

Post a Comment for "Show Android Notification Every Five Minutes"