SensorManager
don’t see much point in telling about taking readings from sensors, SensorEventListener
and SensorManager
classes. cited above two articles, which detail this.onStartCommand
method) @SuppressLint("DefaultLocale") public class SensorLoggerService extends Service implements SensorEventListener { private SensorManager sm; private BufferedOutputStream outStream; private OutputStreamWriter sWriter; private String appDirString; private Calendar cal; private Long startTime; private String date_format = "yyyy-MM-dd_HH-mm-ss"; private Boolean first = true; @Override public int onStartCommand(Intent intent, int flags, int startId) { sm = (SensorManager) getSystemService(SENSOR_SERVICE); appDirString = intent.getStringExtra(MainActivity.APP_DIR); rec_start(); return super.onStartCommand(intent, flags, startId); } public void rec_start() { try { String filename = currentDateToString()+".txt"; File appDir = new File(appDirString); if(!appDir.exists()) appDir.mkdirs(); File file = new File(appDir, filename); outStream = new BufferedOutputStream(new FileOutputStream(file)); Toast.makeText(getApplicationContext(), appDirString+filename,Toast.LENGTH_SHORT).show(); sWriter = new OutputStreamWriter(outStream); Toast.makeText(getApplicationContext(), getString(R.string.record_started),Toast.LENGTH_SHORT).show(); sm.registerListener(this,sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), sm.SENSOR_DELAY_NORMAL); } catch (Throwable t1) { Toast.makeText(getApplicationContext(), "Exception: " + t1.toString(), Toast.LENGTH_LONG).show(); } }
SensorManager
type (variable sm
) is initialized. This class in Android is designed to work with sensors (you can read about it in the following articles in the introduction, as well as in the documentation ).currentDateToString
method, which returns the date and time as a string, is responsible for generating the file name. @SuppressLint("SimpleDateFormat") public String currentDateToString() { SimpleDateFormat sdf = new SimpleDateFormat(date_format); cal = Calendar.getInstance(); return sdf.format(cal.getTime()); }
onSensorChanged
, the onSensorChanged
method is onSensorChanged
, inside which there is a check on which sensor the event occurred, and if it is an accelerometer, then the readings that came are recorded in the file and sent to the Application Activity (a new intention is created and sendBroadcast
function): @Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) writeData(event.values); } @SuppressLint("DefaultLocale") public void writeData(float values[]) { try{ cal = Calendar.getInstance(); if(first) { startTime = cal.getTimeInMillis(); first = false; } Long currentTime = cal.getTimeInMillis()-startTime; String data = Long.toString(currentTime)+String.format(" %f %f %f\n", values[0],values[1],values[2]); sWriter.write(data); Intent intent = new Intent(MainActivity.BROADCAST_ACTION); intent.putExtra(MainActivity.VAL1, values[0]); intent.putExtra(MainActivity.VAL2, values[1]); intent.putExtra(MainActivity.VAL3, values[2]); sendBroadcast(intent); } catch (Throwable t1) { Toast.makeText(getApplicationContext(), "Exception: " + t1.toString(), Toast.LENGTH_SHORT) .show(); } }
sendBroadcast
function. Consider the rec_start
method from the Activity class: public final static String BROADCAST_ACTION = "SensorLoggerServiceRecieve"; public void rec_start() { Intent startServiceIntent = new Intent(this,SensorLoggerService.class) .putExtra(APP_DIR, appDirString); startService(startServiceIntent); rec=true; button_rec_off.setEnabled(true); button_rec_on.setEnabled(false); intentFlt = new IntentFilter(BROADCAST_ACTION); br = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { float val1 = intent.getFloatExtra(VAL1, 0); float val2 = intent.getFloatExtra(VAL2, 0); float val3 = intent.getFloatExtra(VAL3, 0); x_label.setText("X: "+String.valueOf(val1)); y_label.setText("Y: "+String.valueOf(val2)); z_label.setText("Z: "+String.valueOf(val3)); } }; registerReceiver(br, intentFlt); }
button_rec_off, button_rec_on
objects are objects of the Button
class, and x_label, y_label
and z_label
are TextView
.Source: https://habr.com/ru/post/168975/
All Articles