0

i want to call a method after every 5 seconds but it runs only one time when app starts

     super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     Handler delayhandler = new Handler();
     Runnable run = new Runnable() {
        @Override
        public void run() {
            loop();

        }
    };
    delayhandler.postDelayed(run, 5000);
     }

   void loop(){

  Toast.makeText(MainActivity.this, "ooh", Toast.LENGTH_SHORT).show();
  }
robinHood013
  • 209
  • 1
  • 2
  • 15

3 Answers3

1
 Handler delayhandler = new Handler();
     Runnable run = new Runnable() {
        @Override
        public void run() {
            loop();

        }
    };
    delayhandler.postDelayed(run, 5000);
     }

   void loop(){
  Toast.makeText(MainActivity.this, "ooh", Toast.LENGTH_SHORT).show();
delayhandler.postDelayed(run, 5000);
  }
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • you are welcome, glad to help. Do accept the answer, makes it one less question in unanswered list :) . While you code, pls consider doing only as much/what is absolutely necessary and keeping it simple, all the best – Pararth Jan 23 '16 at 07:55
1

create method :

public void setAlarm(Context context, int interva) {
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(context, NotificationAlarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interva, pi); 
}

then create class NotificationAlarm

public class NotificationAlarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        loop();
    }

    private void loop() {
    //work
    }
}

add the class in AndroidManifest

<receiver android:name=".NotificationAlarm" />

call the method in activity or service

setAlarm(this,5000);

for cancelAlarm try this:

public void cancelAlarm(Context context) {

    Intent intent = new Intent(context, NotificationAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmManager = (AlarmManager) G.context.getSystemService(G.context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
}
sajad zohrei
  • 377
  • 1
  • 10
  • even if it would(assuming corrected, working) do what is asked of but that is too much unnecessary work, not needed dependency and inefficient use of broadcast receivers and alarm managers, to achieve what is necessary. Not Recommended.. just saying so you would be aware about it – Pararth Jan 23 '16 at 07:37
  • setAlarm(this,5000); 5000=Interval – sajad zohrei Jan 23 '16 at 07:39
  • I do not understand,Would you explain more? – sajad zohrei Jan 23 '16 at 07:42
  • 1
    @MikeM. thanks.. for the ack and "huge overkill", i tried thinking about terms but couldn't come up with that – Pararth Jan 23 '16 at 07:56
0

you can use Timer task for that as well like this ,

private Timer timer;
private TimerTask timerTask = new TimerTask() {

    @Override
    public void run() {
        final Random random = new Random();
        int i = random.nextInt(2 - 0 + 1) + 0;
        random_note.setImageResource(image[i]);
    }
};

public void start() {
    if(timer != null) {
        return;
    }
    timer = new Timer();
    timer.scheduleAtFixedRate(timerTask, 0, 2000);
}

public void stop() {
    timer.cancel();
    timer = null;
} 
Radhey
  • 2,139
  • 2
  • 24
  • 42