-2

I have a function public void notifiy() this function contains notification and plays sound which can be run while app is not running

my question is I want to call that function for example everyday when current time of device is 6:00 am I know it is done by Alarm Manager but how to call that function at a certain time checking the current time.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

you must use the Android Alarms (AlarmManager)

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast

samples are provided by the official docs : here

EDIT: if you don't want to use the AlarmManager , you can use Timer or ScheduledThreadPoolExecutor as it's mentionned in this answer

timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

    synchronized public void run() {

        \\ here your todo;
        }

    }}, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(1));

ScheduledThreadPoolExecutor is prefered to use :

ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate
  (new Runnable() {
     public void run() {
        // call service
     }
  }, 0, 10, TimeUnit.MINUTES);
Community
  • 1
  • 1