制作一个"设备的温度实时图表"

时间:2015-09-21 03:19:35

标签: android graph battery android-graphview batterymanager

我正在制作设备的温度实时图表。但是图表中的温度始终为0(我确实有BATTERY_STATS权限)。

The blue line is the graph

private final Handler mHandler = new Handler();
private Runnable mTimer1;
private Runnable mTimer2;
private LineGraphSeries<DataPoint> mSeries1;
private LineGraphSeries<DataPoint> mSeries2;
private double graph2LastXValue = 5d;
private int temp;

这是我的BroadcastReceiver:

 private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent intent) {

        try {
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
        } catch (Exception e) {
            //...
        }
    }
};

这是我的OnCreateView

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab_2,container,false);

    getActivity().registerReceiver(this.mBatInfoReceiver,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    GraphView graph2 = (GraphView) v.findViewById(R.id.graph2);
    mSeries2 = new LineGraphSeries<DataPoint>();
    graph2.addSeries(mSeries2);
    graph2.getViewport().setXAxisBoundsManual(true);
    graph2.getViewport().setMinX(0);
    graph2.getViewport().setMaxX(40);

    return v;
}

这是我的......图表代码(?)(我不太确定:D)

@Override
public void onResume() {
    super.onResume();


    mTimer2 = new Runnable() {
        @Override
        public void run() {
            graph2LastXValue += 1d;
            mSeries2.appendData(new DataPoint(graph2LastXValue, temp/10f), true, 40);
            mHandler.postDelayed(this, 200);
        }
    };
    mHandler.postDelayed(mTimer2, 1000);
}

@Override
public void onPause() {
    //...
}
}

我只是一个新手,所以请帮助我!我是通过jjoe64使用GraphView。

0 个答案:

没有答案