Restarting activity at a specific time

I am working on an application where I need to refresh / restart my menu activity at a specific time. for example, 12 o'clock in the afternoon. How can i do this. Note: the menupage needs to be restarted at 12 o'clock if the user is using my app before 12 o'clock with the old menu and it has passed 12 hours while using my app. The application does not need to be restarted if the application is closed. Its as long as the client is using my application during this period before 12 noon and after 12 pm, as my menu changes after 12 noon. Thus, the user needs to see the updated menu after 12 noon.

+3


source to share


2 answers


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

0


source


+1


source







All Articles