3

I am working on an app where I need to refresh/restart my menuPage activity at a particular time. for example 12pm in afternoon. How can I go about to achieve it. Note : The menupage needs to restart at 12 pm if the user is using my app before 12 pm with the old menu and he has passed 12 pm time while using my app. The app need not restart if the app is closed. Its while the customer using my app during that period of before 12pm and after 12 pm since my menu changes after 12pm in afternoon. So the user needs to see the updated menu after 12 pm

souravlahoti
  • 716
  • 2
  • 8
  • 29
  • Please read these links - http://developer.android.com/training/basics/activity-lifecycle/stopping.html. - https://stackoverflow.com/questions/28807029/how-to-start-activity-after-some-time-in-android - https://stackoverflow.com/questions/13230996/how-to-start-an-activity-after-certain-time-period – IntelliJ Amiya Apr 20 '15 at 11:02

1 Answers1

0

This will work for sure, 100%...

final long delayMillis=1000;
Handler h=null;
Runnable r;

in onCreate()

h=new Handler(Looper.getMainLooper());
    r = new Runnable() {

           public void run() {

               //current time
               Calendar c = Calendar.getInstance(); 
                int hour = c.get(Calendar.HOUR_OF_DAY);
                int min=c.get(Calendar.MINUTE);
                int sec=c.get(Calendar.SECOND);
                String currenttime= String.valueOf(hour)+" : "+String.valueOf(min)+" : "+String.valueOf(sec);


             //comparing current time with 12:00pm
               if(currenttime.equals("12 : 0 : 0")){

                 //restarting the activity
                   Intent intent = getIntent();
                   finish();
                   startActivity(intent);

               }


            h.postDelayed(this, delayMillis);

        }
      };

    h.post(r);

All the best..

Prakhar
  • 710
  • 6
  • 24
  • This may not work in some cases if there's a >1sec period where your thread isn't running right at the target time (e.g., lagspike or your app is backgrounded). It would also be much more efficient to simply figure out how far in the future the target time is and calculate the needed delay. Finally, the `post` is pointless: `onCreate` is already on the main thread. – Ryan M Mar 01 '23 at 01:39