Android: Repeat a method and delay other method after x seconds using handle -
i'm new android , have problem used same code repeat , delay method 2 class. 1 class work fine other not. don't know why. code
speedmeterfragment.java
public class speedmeterfragment extends fragment { .... public void speedmeterbefore() { totalrxbytesbefore = trafficstats.gettotalrxbytes(); log.d("test", "before: " + string.valueof(totalrxbytesbefore)); } public void speedmeterafter() { totalrxbytesafter = trafficstats.gettotalrxbytes(); log.d("test", "after: " + string.valueof(totalrxbytesafter)); } public void speedmeterdifference() { totalrxbytesdifference = totalrxbytesafter - totalrxbytesbefore; tvtest.settext(string.valueof(totalrxbytesdifference/1024) + " kb/s"); log.d("test", "difference: " + string.valueof(totalrxbytesdifference)); } public void speedmeter() { handler = new handler(); handler.post(runnable = new runnable() { @override public void run() { speedmeterbefore(); final handler handler1 = new handler(); handler1.postdelayed(new runnable() { @override public void run() { speedmeterafter(); speedmeterdifference(); } }, 1000); handler.postdelayed(this, 1000); } }); } }
and savedatauseage.java
public class savedatauseage extends broadcastreceiver { ... public void onreceive(context context, intent intent) { ... savepreference(); } public void savepreference() { ... measuredatamb(); } public void measuredatambbefore() { datambbefore = trafficstats.gettotalrxbytes() / 1048576; } public void measuredatambafter() { datambafter = trafficstats.gettotalrxbytes() / 1048576; } public void measuredatambdifference() { datambdifference = datambafter - datambbefore; } public void measuredatamb() { handler = new handler(); handler.post(runnable = new runnable() { @override public void run() { measuredatambbefore(); final handler handler1 = new handler(); handler1.postdelayed(new runnable() { @override public void run() { measuredatambafter(); measuredatambdifference(); } }, 1000); handler.postdelayed(this, 1000); } }); } }
speedmeterfragment work fine, not savedatauseage me? sorry bad english :)
handler = new handler(); final runnable r = new runnable() { public void run() { //your method called here handler.postdelayed(this, 1000); } }; handler.postdelayed(r, 1000);
this code repeat method after every second
Comments
Post a Comment