我正在制作设备的温度实时图表。但图中的温度始终为 0(我确实有 BATTERY_STATS 权限)。
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;
这是我的广播接收器:
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。