Home > Mobile >  How to call onClick method every x hour or minutes? [Android]
How to call onClick method every x hour or minutes? [Android]

Time:04-24

I'm beginner. In my application I have a button which get a stats from class to MainActivity and set text on textview. In method onClick are other methods call too. Everything works fine, I can click the button manually and get all information, but I want to add automatic call onClick method for example in every 1 hour. I read about AlarmManager and Timer to do that, but I don't know how to implement it in my case.

Below code with method onClick. It's on MainActivity in OnCreate.

statsBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UStats.printCurrentUsageStatus(MainActivity2.this);

            long time = UStats.printCurrentUsageStatus(MainActivity2.this);

            if (time >= 300000)
            {
                sendOnChannel1();
            }

            String total="";
            long sec=time/1000;
            long day;
            long hour;
            long min;
            if(sec>=(86400)){
                day=sec/86400;
                sec=sec           
  • Related