如何使文本视图显示另一种方法的温度数据?

时间:2015-05-15 03:09:20

标签: android text methods textview temperature

以下是该应用的完整代码。我不相信我有一个游戏方法:

package com.solomonreedstudios.thermometer;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;


public class Main extends ActionBarActivity implements  SensorEventListener {
private SensorManager mSensorManager;
private Sensor mTemperature;

public TextView tv1;
public TextView tv2;

public TextView tv3;


SoundPool beepInt;
int beep;


@Override
public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sensor_actvity);
    tv3 = (TextView) findViewById(R.id.mTextField);

    beepInt = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    beep = beepInt.load(this, R.raw.beep, 1);

    // Get an instance of the sensor service, and use that to get an instance of
    // a particular sensor.
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mTemperature = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
}

@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
}

@Override
public final void onSensorChanged(SensorEvent event) {
    // Do something with this sensor data.
    float lux = event.values[0];
    float fahrenheit = lux * 9 / 5 + 32;
    tv1 = (TextView) findViewById(R.id.text1);
    tv1.setText(String.valueOf(lux));
    tv2 = (TextView) findViewById(R.id.textView2);
    tv2.setText(String.valueOf(fahrenheit));
}
@Override
protected void onResume() {
    //  register  a listener for the sensor.
    super.onResume();
    mSensorManager.registerListener(this, mTemperature, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    // Be sure to unregister the sensor when the ACTIVITY pauses.
    super.onPause();
    mSensorManager.unregisterListener(this);
}



public final void collectData(View view) {


    new CountDownTimer(300, 1000) {

        public void onTick(long millisUntilFinished) {
            tv3.setText("seconds remaining until reading: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            beepInt.play(beep, 1, 1, 1, 0, 1);
            tv3.setText("Your Temperature Is  " + mTemperature);

        }
    }.start();

}

     }

这有用吗?

0 个答案:

没有答案