Sample Code - Implementing the sensor in Android
Minggu, 22 Juli 2012
0
komentar
Sample Code - TYPE_LIGHT sensor to get the screen brightness for android device
Android provide the android.hardware framework to implement the sensors. you could get a quick understanding of android sensor framework for our previous post.
http://creativeandroidapps.blogspot.in/2012/07/basics-of-sensors-on-android-platform.html
This post will explain the implementation of SensorEventListener for the TYPE_LIGHT sensor. The light listener is used to control the brightness of the screen.
You need to create a Android Application Project in your eclipse editor and modify the Activity class to extend with SensorEventListener. See the following code of the sample.
package com.example.samplesensor;
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.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent arg0) {
// TODO Auto-generated method stub
float lux = arg0.values[0];
Toast.makeText(getApplicationContext(), "Sensor Value " + lux, 1000).show();
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
Get the instance of the system service of sensor manager and get the TYPE_LIGHT sensor from the system.
You need to implement the two callback methods provided by the SessionEventListener.
The sensor need to register on the onResume() and onPause() methods of activity class.
The TYPE_LIGHT sensor only returns the one value which is brightness of the device screen. The brightness of the screen is measured by lux unit.
One lux is equal to one lumen per square metre.
If you like the example please provide your feedback.
Thanks
Creative Android Apps
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Sample Code - Implementing the sensor in Android
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://apk-xda.blogspot.com/2012/07/sample-code-implementing-sensor-in.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar