Setting temperature through emulator

孤人 提交于 2019-12-19 10:38:08

问题


I run this code on the emulator to read temperature change, but it never detects the temperature change at onSensorChanged(). I use this command to change temperature through the telnet tool:

sensor set temperature 1:2:3

What did I do wrong?

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);
  }
}

Thanks


回答1:


When you created the emulator, did you give it the hardware? Open AVD, look for the Hardware section, click "New" and add "Temperature Support."




回答2:


This worked for me:

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]));
    }
}

as a note I say that Sensor.TYPE_AMBIENT_TEMPERATURE is not working in my AVD emulator.

I change the temperature following the instructions Using the Android Emulator, for example telnet localhost 5554 and then sensor set temperature -0.5.

I have no particular permission and I did not find the "Hardware section" JustinDanielson was talking about in his answer.

The manifest:

<?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>


来源:https://stackoverflow.com/questions/10377690/setting-temperature-through-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!