All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE

All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE - Welcome to ANDROID KAWE, Ini adalah sebuah blog directory tentang burung,baik itu suara burung ataupun tips perawatan burung.Blog ini adalah situs perangkum otomatis dari pencarian google yang kali ini berjudul All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE.Ada ribuan artikel yang sudah terekam ke dalam situs ini,silahkan cari sesuai yang kalian kehendaki,untuk artikel selanjutnya tentang judul diatas bisa kalian baca di bawah ini.

Konten : All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE
Judul : All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE

lihat juga


All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE


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




Demikianlah Artikel All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE

Semoga artikel tentang All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE, mudah-mudahan bisa memberi manfaat untuk anda semua.

Anda sedang membaca artikel All About Android-Sample Code - Implementing the sensor in Android-ANDROIDKAWE dan artikel ini url permalinknya adalah http://androidkawe.blogspot.com/2012/07/all-about-android-sample-code.html Semoga artikel ini bisa bermanfaat.Sekali lagi,ini adalah situs auto yang tidak ditulis langsung oleh admin,Kami tidak menjamin akan kebenaran dari artikel yang tertulis.
Back To Top