Runnable and postdelayed in android 4.0 (GraphView Library for real time) -
im using graphview library visualize real time graphs. library example works fine in 2.x android devices, doesnt work in 4.x devices. graph refreshed when click screen. im newbie in android think problem use of runnable , postdelayed.
this example code:
package com.example.realtime; import com.jjoe64.graphview.bargraphview; import com.jjoe64.graphview.graphview; import com.jjoe64.graphview.graphview.graphviewdata; import com.jjoe64.graphview.graphview.legendalign; import com.jjoe64.graphview.graphviewseries; import com.jjoe64.graphview.linegraphview; import android.app.activity; import android.os.bundle; import android.os.handler; import android.widget.linearlayout; public class realtimegraph extends activity { private final handler mhandler = new handler(); private runnable mtimer1; private runnable mtimer2; private graphview graphview; private graphviewseries exampleseries1; private graphviewseries exampleseries2; private double graph2lastxvalue = 5d; private double getrandom() { double high = 3; double low = 0.5; return math.random() * (high - low) + low; } /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.graphs); // init example series data exampleseries1 = new graphviewseries(new graphviewdata[] { new graphviewdata(1, 2.0d) , new graphviewdata(2, 1.5d) , new graphviewdata(2.5, 3.0d) // frequency , new graphviewdata(3, 2.5d) , new graphviewdata(4, 1.0d) , new graphviewdata(5, 3.0d) }); // graph dynamically genereated horizontal , vertical labels if (getintent().getstringextra("type").equals("bar")) { graphview = new bargraphview( // context , "graphviewdemo" // heading ); } else { graphview = new linegraphview( // context , "graphviewdemo" // heading ); } graphview.addseries(exampleseries1); // data linearlayout layout = (linearlayout) findviewbyid(r.id.graph1); layout.addview(graphview); // ---------- exampleseries2 = new graphviewseries(new graphviewdata[] { new graphviewdata(1, 2.0d) , new graphviewdata(2, 1.5d) , new graphviewdata(2.5, 3.0d) // frequency , new graphviewdata(3, 2.5d) , new graphviewdata(4, 1.0d) , new graphviewdata(5, 3.0d) }); // graph custom labels , drawbackground if (getintent().getstringextra("type").equals("bar")) { graphview = new bargraphview( , "graphviewdemo" ); } else { graphview = new linegraphview( , "graphviewdemo" ); ((linegraphview) graphview).setdrawbackground(true); } graphview.addseries(exampleseries2); // data graphview.setviewport(1, 4); graphview.setscalable(true); layout = (linearlayout) findviewbyid(r.id.graph2); layout.addview(graphview); } @override protected void onpause() { mhandler.removecallbacks(mtimer1); mhandler.removecallbacks(mtimer2); super.onpause(); } @override protected void onresume() { super.onresume(); mtimer1 = new runnable() { @override public void run() { exampleseries1.resetdata(new graphviewdata[] { new graphviewdata(1, getrandom()) , new graphviewdata(2, getrandom()) , new graphviewdata(2.5, getrandom()) // frequency , new graphviewdata(3, getrandom()) , new graphviewdata(4, getrandom()) , new graphviewdata(5, getrandom()) }); mhandler.postdelayed(this, 300); } }; mhandler.postdelayed(mtimer1, 300); mtimer2 = new runnable() { @override public void run() { graph2lastxvalue += 1d; exampleseries2.appenddata(new graphviewdata(graph2lastxvalue, getrandom()), true); mhandler.postdelayed(this, 1000); } }; mhandler.postdelayed(mtimer2, 1000); }
}`
the important code inside function onresume.
id appreciate hint...
thanks in advance!
i don;t understand why call posdelayed on handler twice - once inside runnable , once after defining runnable...
however , try invalidate() view once it's updated. :
@override protected void onresume() { super.onresume(); mtimer1 = new runnable() { @override public void run() { exampleseries1.resetdata(new graphviewdata[] { new graphviewdata(1, getrandom()) , new graphviewdata(2, getrandom()) , new graphviewdata(2.5, getrandom()) // frequency , new graphviewdata(3, getrandom()) , new graphviewdata(4, getrandom()) , new graphviewdata(5, getrandom()) }); exampleseries1.invalidate(); } }; mhandler.postdelayed(mtimer1, 300); mtimer2 = new runnable() { @override public void run() { graph2lastxvalue += 1d; exampleseries2.appenddata(new graphviewdata(graph2lastxvalue, getrandom()), true); exampleseries2.invalidate(); } }; mhandler.postdelayed(mtimer2, 1000); }
let me know if works you
Comments
Post a Comment