我的广播接收器和发送器在这里搞砸了一些东西。也许另一双眼睛会很有用。
我有我的光传感器,它应该广播接收活动的变化。
这是 LightSensor.java
public void onSensorChanged(SensorEvent event) {
lightLux = event.values[0]; //Final output of this sensor.
Lux = String.valueOf(lightLux);
sendLuxUpdate();
Log.d("LightSensor", Lux);
TextView tvLightSensorLux = (TextView) findViewById(R.id.tvLightSensorLux);
tvLightSensorLux.setText(Lux);
}
private void sendLuxUpdate() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("LuxUpdate");
// You can also include some extra data.
intent.putExtra("Lux", lightLux);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
然后我的 Record.java 应该接收这些对 lux 的更新:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
LocalBroadcastManager.getInstance(this).registerReceiver(mLightReceiver,
new IntentFilter("LuxUpdate"));
}
private BroadcastReceiver mLightReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String lux = intent.getStringExtra("Lux");
Log.d("Light Lux", "Lux Update: " + lux);
TextView tvSensorLightLux = (TextView) findViewById(R.id.tvSensorLightLux);
tvSensorLightLux.setText(lux);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mLightReceiver);
super.onDestroy();
}
我认为这只是发送和接收 id 的问题,但我并不完全确定。一旦 Record 活动接收到广播,它应该更新 TextView tvSensorLightLux 或至少 Log.d 来自 LightSensor.java 的 Lux 值