I am using code below to schedule a task in Android but it is not giving any results. Please advise on that.
int delay = 5000; // Delay int period for 5 seconds = 1000; // every second repeat timer timer = new timer (); Timer.scheduleAtFixedRate (New TimerTask) {Public Zero Run () {Toast. Make text (getApplicationContext (), "run!", Toast .LENGTH_SHORT). Show ();}}, delay, duration); Timer Tasks are not ideal for use in an Android environment because they are context- Keep in mind. If your reference goes away, the timer task will still patiently wait in the background, eventually firing and potentially crash your app because its activity had already ended or it could stop the context of your activity After this, it can stop collecting garbage and potentially make your app out of memory. Instead, use post delimited (), which will automatically cancel the task when the function is closed.
final full delays = 5000; Last int duration = 1000; Final Runnabal R = New Runnabal () {Public Zero Run (Toast). Make Text (getApplicationContext (), "RAN!", Toast. LNNGHHORT). Show (); Post delimited (this, duration); }}; Post delayed (r, delay);
By the way, if you ever need to cancel your work manually, you can use removeClallback (r)
where r already Posted.
P>
Comments
Post a Comment