2

我在模拟器上运行此代码以读取温度变化,但它从未检测到温度变化onSensorChanged()。我使用这个命令通过 telnet 工具改变温度:

传感器设定温度 1:2:3

我做错什么了?

public class SensorActivity extends Activity implements SensorEventListener {
  private SensorManager mSensorManager;
  private Sensor mysensor;

  @Override
  public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mysensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);

  }
    public void printText(String text)
    {

        EditText et=(EditText)findViewById(R.id.editText1);
        et.setText(text);

    }
  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
      String text=sensor.getName()+"accuracy = " + accuracy;
      printText(text);
  }

  @Override
  public final void onSensorChanged(SensorEvent event) {

    String text = "OnSensorChanged";
    printText(text);
  }

  @Override
  protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mysensor, SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
}

谢谢

4

2 回答 2

1

当你创建模拟器时,你给它硬件了吗?打开 AVD,查找硬件部分,单击“新建”并添加“温度支持”。

于 2012-04-30T02:45:47.520 回答
0

这对我有用:

package com.example.test_sensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {
    public void make_a_toast(String s)
    {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, s, duration);
        toast.show();
    }

    private SensorManager mSensorManager;
    private Sensor mTemp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mTemp = mSensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mTemp, SensorManager.SENSOR_DELAY_FASTEST);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {
        make_a_toast(Float.toString(event.values[0]));
    }
}

作为注释,我说这Sensor.TYPE_AMBIENT_TEMPERATURE在我的 AVD 模拟器中不起作用。

例如,我按照使用 Android 模拟器的说明更改温度telnet localhost 5554,然后sensor set temperature -0.5

我没有特别许可,也没有找到贾斯汀丹尼尔森在他的回答中所说的“硬件部分” 。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test_sensor"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test_sensor.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
于 2013-02-05T19:26:01.900 回答