0

ここで、ブロードキャスト レシーバーとセンダーを使って何かを間違えました。おそらく、別の目のセットが役立つでしょう。

受信アクティビティへの変更をブロードキャストするはずの光センサーがあります。

ここに 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と受信IDの問題だと思いますが、よくわかりません。Record アクティビティがブロードキャストを受信したら、TextView tvSensorLightLux を更新するか、少なくとも LightSensor.java からの Lux 値を Log.d する必要があります。

4

1 に答える 1

0

Float を渡しており、使用しているレシーバーでは、

String lux = intent.getStringExtra("Lux");

"Lux"Stringである必要があります。

あなたがフロートを通過しているように。レシーバーに追加するだけgetFloatExtra()

lux =intent.getFloatExtra("Lux", defaultValue); 

必要なデフォルト値を入力してください0

于 2014-01-13T12:15:18.473 に答える