Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package com.shaikhhamadali.blogspot.lightsensor;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.hardware.Sensor;
  6. import android.hardware.SensorEvent;
  7. import android.hardware.SensorEventListener;
  8. import android.hardware.SensorManager;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.ListView;
  16. import android.widget.TextView;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. public class LightSensor extends Activity implements SensorEventListener {
  22.  
  23. //declare Variables
  24. private SensorManager sensorManager;
  25.  
  26. private List<Sensor> sensorList;
  27. private ListView lv;
  28. private List<SensorDataInterface> formatedSensors;
  29.  
  30.  
  31. private ArrayAdapter adapter;
  32.  
  33. private LocationManager mLocationManager;
  34. private final LocationListener mLocationListener = new LocationListener() {
  35. @Override
  36. public void onLocationChanged(final Location location) {
  37. Log.i("Message: ", "Location changed, " + location.getAccuracy() + " , " + location.getLatitude() + "," + location.getLongitude());
  38. formatedSensors.set(formatedSensors.size() - 1, new gpsData(location.getLatitude(), location.getLongitude()));
  39. adapter.notifyDataSetChanged();
  40. }
  41.  
  42. @Override
  43. public void onStatusChanged(String s, int i, Bundle bundle) {
  44.  
  45. }
  46.  
  47. @Override
  48. public void onProviderEnabled(String s) {
  49.  
  50. }
  51.  
  52. @Override
  53. public void onProviderDisabled(String s) {
  54.  
  55. }
  56. };
  57.  
  58. /**
  59. * Called when the activity is first created.
  60. */
  61. @Override
  62. public void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.activity_light_sensor);
  65.  
  66. //Sensors
  67. sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  68. formatedSensors = new ArrayList<SensorDataInterface>();
  69. sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
  70. for (Sensor s : sensorList) {
  71. sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
  72. formatedSensors.add(new SensorData(s.getName(), s.getType(), s.getMaximumRange(), 0));
  73. }
  74.  
  75. //GPS
  76. mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  77. mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
  78. 10, mLocationListener);
  79.  
  80. //Collecting results
  81. formatedSensors.add(new gpsData(0, 0));
  82. lv = (ListView) findViewById(R.id.listView1);
  83. adapter = new ArrayAdapter<SensorDataInterface>(this, android.R.layout.simple_list_item_1, formatedSensors);
  84. lv.setAdapter(adapter);
  85. }
  86.  
  87.  
  88. @Override
  89. public void onSensorChanged(SensorEvent event) {
  90. for (Sensor s : sensorList) {
  91. if (event.sensor.getType() == s.getType()) {
  92. for (SensorDataInterface sd : formatedSensors) {
  93. if (!(sd instanceof gpsData) && ((SensorData) sd).getType() == s.getType()) {
  94. ((SensorData) sd).setValue(event.values[0]);
  95. }
  96. }
  97. }
  98. }
  99. adapter.notifyDataSetChanged();
  100. }
  101.  
  102.  
  103. @Override
  104. protected void onResume() {
  105. super.onResume();
  106. for (Sensor s : sensorList) {
  107. sensorManager.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
  108. }
  109. }
  110.  
  111. @Override
  112. protected void onPause() {
  113. super.onPause();
  114. sensorManager.unregisterListener(this);
  115. }
  116.  
  117. @Override
  118. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  119.  
  120. }
  121.  
  122. }
  123.  
  124. interface SensorDataInterface {
  125. }
  126.  
  127. class gpsData implements SensorDataInterface {
  128. private double latitude;
  129. private double longitude;
  130.  
  131. public gpsData(double latitude, double longitude) {
  132. this.latitude = latitude;
  133. this.longitude = longitude;
  134. }
  135.  
  136. public double getLatitude() {
  137. return latitude;
  138. }
  139.  
  140. public void setLatitude(double latitude) {
  141. this.latitude = latitude;
  142. }
  143.  
  144. public double getLongitude() {
  145. return longitude;
  146. }
  147.  
  148. public void setLongitude(double longitude) {
  149. this.longitude = longitude;
  150. }
  151.  
  152. public String toString() {
  153. return "GPS data:\n" +
  154. "Latitude: " + getLatitude() + "\n" +
  155. "Longtitude: " + getLongitude();
  156. }
  157. }
  158.  
  159. class SensorData implements SensorDataInterface {
  160. private String name;
  161. private int type;
  162. private float range;
  163. private float value;
  164.  
  165. public SensorData(String name, int type, float range, int value) {
  166. this.name = name;
  167. this.type = type;
  168. this.range = range;
  169. this.value = value;
  170. }
  171.  
  172. public String getName() {
  173. return name;
  174. }
  175.  
  176. public void setName(String name) {
  177. this.name = name;
  178. }
  179.  
  180. public int getType() {
  181. return type;
  182. }
  183.  
  184. public void setType(int type) {
  185. this.type = type;
  186. }
  187.  
  188. public float getRange() {
  189. return range;
  190. }
  191.  
  192. public void setRange(int range) {
  193. this.range = range;
  194. }
  195.  
  196. public float getValue() {
  197. return value;
  198. }
  199.  
  200. public void setValue(float value) {
  201. this.value = value;
  202. }
  203.  
  204. public String toString() {
  205. return "Name: \n" +
  206. "Range: " + getRange() + "\n" +
  207. "Value: " + getValue();
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement