Home
› Uncategorized
The temperature sensors (Sensor.TYPE_TEMPERATURE/Sensor.TYPE_AMBIENT_TEMPERATURE) are used to determine temperature of the phone, for internal hardware. They are not available on all device. (It's not available on my HTC One X and HTC Flyer!)
Sensor.TYPE_TEMPERATURE is deprecated, use Sensor.TYPE_AMBIENT_TEMPERATURE instead.
Example:
Related:
- Determine light level, Sensor.TYPE_LIGHT.
Sensor.TYPE_TEMPERATURE is deprecated, use Sensor.TYPE_AMBIENT_TEMPERATURE instead.
Example:
package com.example.androidsensor; 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.widget.TextView; public class MainActivity extends Activity { TextView textTEMPERATURE_available, textTEMPERATURE_reading; TextView textAMBIENT_TEMPERATURE_available, textAMBIENT_TEMPERATURE_reading; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textTEMPERATURE_available = (TextView)findViewById(R.id.TEMPERATURE_available); textTEMPERATURE_reading = (TextView)findViewById(R.id.TEMPERATURE_reading); textAMBIENT_TEMPERATURE_available = (TextView)findViewById(R.id.AMBIENT_TEMPERATURE_available); textAMBIENT_TEMPERATURE_reading = (TextView)findViewById(R.id.AMBIENT_TEMPERATURE_reading); SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); Sensor TemperatureSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE); if(TemperatureSensor != null){ textTEMPERATURE_available.setText("Sensor.TYPE_TEMPERATURE Available"); mySensorManager.registerListener( TemperatureSensorListener, TemperatureSensor, SensorManager.SENSOR_DELAY_NORMAL); }else{ textTEMPERATURE_available.setText("Sensor.TYPE_TEMPERATURE NOT Available"); } Sensor AmbientTemperatureSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); if(AmbientTemperatureSensor != null){ textAMBIENT_TEMPERATURE_available.setText("Sensor.TYPE_AMBIENT_TEMPERATURE Available"); mySensorManager.registerListener( AmbientTemperatureSensorListener, AmbientTemperatureSensor, SensorManager.SENSOR_DELAY_NORMAL); }else{ textAMBIENT_TEMPERATURE_available.setText("Sensor.TYPE_AMBIENT_TEMPERATURE NOT Available"); } } private final SensorEventListener TemperatureSensorListener = new SensorEventListener(){ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_TEMPERATURE){ textTEMPERATURE_reading.setText("TEMPERATURE: " + event.values[0]); } } }; private final SensorEventListener AmbientTemperatureSensorListener = new SensorEventListener(){ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE){ textAMBIENT_TEMPERATURE_reading.setText("AMBIENT TEMPERATURE: " + event.values[0]); } } }; }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" tools:context=".MainActivity" /> <TextView android:id="@+id/TEMPERATURE_available" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TEMPERATURE_reading" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/AMBIENT_TEMPERATURE_available" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/AMBIENT_TEMPERATURE_reading" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Related:
- Determine light level, Sensor.TYPE_LIGHT.
-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
Babel Rising 3D v1.7.1 APKBabel Rising 3D v1.7.1 APK Google play Store Go To Adf.ly Your divine intervent…
-
Tap Tap Revenge 4A lot of user request this so here's how to install Tap Tap Revenge 4 *credit goes to everyone …
-
[Gameloft] Zombie Wood S40, S60v3, S60v5 & AndroidZombiewood is the latest shooter game for Android devices as well as Symbian Phones from Gameloft …
-
Game HD Modern Combat Sandstorm HVGA for Android PhoneGame HD Modern Combat Sandstorm HVGA for Android Phone Modern Combat Sandstorm HD is a war game th…
-
Text Bomber for AndroidText Bomber for Android In a previous post I've ever shared about SMS Bomber Application for An…
-
iWindows v1.51 S60v5iWindows v1.51 S60v5 Support/Info : Symbian^1^3 Anna Belle UnSigned Retail by DSPDA™ Details : iWin…
-
[PC Game] Naruto Ninja Memories [mediafire]Naruto: Ninja Memories 1.1 (PC/2010) | 4.58 GB Genre: Fighting/Arcade | Developer: NarutoPlanet |…
Post a Comment
Post a Comment