Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.shaikhhamadali.blogspot.lightsensor;
- import android.app.Activity;
- import android.content.Context;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class LightSensor extends Activity implements SensorEventListener {
- //declare Variables
- private SensorManager sensorManager;
- private List<Sensor> sensorList;
- private ListView lv;
- private List<SensorDataInterface> formatedSensors;
- private ArrayAdapter adapter;
- private LocationManager mLocationManager;
- private final LocationListener mLocationListener = new LocationListener() {
- @Override
- public void onLocationChanged(final Location location) {
- Log.i("Message: ", "Location changed, " + location.getAccuracy() + " , " + location.getLatitude() + "," + location.getLongitude());
- formatedSensors.set(formatedSensors.size() - 1, new gpsData(location.getLatitude(), location.getLongitude()));
- adapter.notifyDataSetChanged();
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- }
- @Override
- public void onProviderEnabled(String s) {
- }
- @Override
- public void onProviderDisabled(String s) {
- }
- };
- /**
- * Called when the activity is first created.
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_light_sensor);
- //Sensors
- sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- formatedSensors = new ArrayList<SensorDataInterface>();
- sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
- for (Sensor s : sensorList) {
- sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
- formatedSensors.add(new SensorData(s.getName(), s.getType(), s.getMaximumRange(), 0));
- }
- //GPS
- mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
- 10, mLocationListener);
- //Collecting results
- formatedSensors.add(new gpsData(0, 0));
- lv = (ListView) findViewById(R.id.listView1);
- adapter = new ArrayAdapter<SensorDataInterface>(this, android.R.layout.simple_list_item_1, formatedSensors);
- lv.setAdapter(adapter);
- }
- @Override
- public void onSensorChanged(SensorEvent event) {
- for (Sensor s : sensorList) {
- if (event.sensor.getType() == s.getType()) {
- for (SensorDataInterface sd : formatedSensors) {
- if (!(sd instanceof gpsData) && ((SensorData) sd).getType() == s.getType()) {
- ((SensorData) sd).setValue(event.values[0]);
- }
- }
- }
- }
- adapter.notifyDataSetChanged();
- }
- @Override
- protected void onResume() {
- super.onResume();
- for (Sensor s : sensorList) {
- sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- sensorManager.unregisterListener(this);
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
- }
- interface SensorDataInterface {
- }
- class gpsData implements SensorDataInterface {
- private double latitude;
- private double longitude;
- public gpsData(double latitude, double longitude) {
- this.latitude = latitude;
- this.longitude = longitude;
- }
- public double getLatitude() {
- return latitude;
- }
- public void setLatitude(double latitude) {
- this.latitude = latitude;
- }
- public double getLongitude() {
- return longitude;
- }
- public void setLongitude(double longitude) {
- this.longitude = longitude;
- }
- public String toString() {
- return "GPS data:\n" +
- "Latitude: " + getLatitude() + "\n" +
- "Longtitude: " + getLongitude();
- }
- }
- class SensorData implements SensorDataInterface {
- private String name;
- private int type;
- private float range;
- private float value;
- public SensorData(String name, int type, float range, int value) {
- this.name = name;
- this.type = type;
- this.range = range;
- this.value = value;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getType() {
- return type;
- }
- public void setType(int type) {
- this.type = type;
- }
- public float getRange() {
- return range;
- }
- public void setRange(int range) {
- this.range = range;
- }
- public float getValue() {
- return value;
- }
- public void setValue(float value) {
- this.value = value;
- }
- public String toString() {
- return "Name: \n" +
- "Range: " + getRange() + "\n" +
- "Value: " + getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement