在函数中具有处理程序是否有意义?

时间:2019-02-15 16:41:15

标签: android function android-handler

我正在使用蓝牙和graphview编写应用程序以查看加速度计数据。为了绘制每组数据点的图形,我创建了一个函数,该函数获取数据和应该在其上的折线图,并设置了一个处理程序来定期执行工作,但这并不使我感到惊讶。使用3个处理程序更合适还是可以?处理程序应该使用post代替吗?哪种方法更有效?

public void addEntry(final String data, final LineGraphSeries Series){
        // does there need to be a runnable in a function or can we just do one?
        graphHandle.postDelayed(new Runnable() {
            @Override
            public void run() {
                float dataVal = 0;
                DataPoint point;
                float current = (System.currentTimeMillis() - start)/1000;
                try {

                dataVal = Float.parseFloat(data);
                Series.appendData(point = new DataPoint(current ,dataVal), true, 20); // Data point, scroll to end, max data points
                //lastX = lastX + 1;
                Log.i("Time", "" + current);
                Log.d("Add Entry", "" + Series.toString());
                Log.d("dataVal1", "" + dataVal);
                Log.i("Datapoint1", "Point " + point.toString());

                } catch (Exception e) {
                e.printStackTrace();
                Log.d("STR -> INT", "int dataVal contained string");
                Log.d("dataVal2", "" + dataVal);
                }
            point = null;
            Log.i("Datapoint2", "Point " + point);
        }
    }, 200);

}

0 个答案:

没有答案