简体   繁体   中英

Android app - save sensor data to local file

I'm doing a project to record data from an accelerometer sensor (xyz) and save it to a file on my phone. I found a code snippet which retrieves the data from the connected sensor but doesnt save it on the found. Unfortunately I dont know Java at all so I was hoping someone here a tip what to add to achive the saving part. I assume I need to continuously append the variables from onSensorChanged (variables accx, accy, accz) to a object and then save it when I stop recording.

EDIT: I declared the following array lists. 3 for my sensor data and one to write the corresponding timestamps.

final ArrayList<Integer> arrlist_ts = new ArrayList<Integer>(5);
final ArrayList<Integer> arrlist_x = new ArrayList<Integer>(5);
final ArrayList<Integer> arrlist_y = new ArrayList<Integer>(5);
final ArrayList<Integer> arrlist_z = new ArrayList<Integer>(5);

I placed the add-to-array commands in the run() function right after the variable String out_text.

I then put the string concatenation in the function stopRecording() in the if (timer1 != null) condition. Does that make sense? I would then put the code to save the strings to a file at the same location.

Here is the code

package pervasive_recorder;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;

import android.os.Handler;
import android.os.Message;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;


public class ARFFRecorderService extends Service implements SensorEventListener {

    private int NOTIFICATION = R.string.recording;

    private final IBinder mBinder = new LocalBinder();
    private NotificationManager mNM;
    private boolean recording = false;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ARFFRecorderActivity clientActivity = null;
    private int ts, accx, accy, accz;
    static Timer timer1 = null;

    final ArrayList<Integer> arrlist_ts = new ArrayList<Integer>(5);
    final ArrayList<Integer> arrlist_x = new ArrayList<Integer>(5);
    final ArrayList<Integer> arrlist_y = new ArrayList<Integer>(5);
    final ArrayList<Integer> arrlist_z = new ArrayList<Integer>(5);

    public class LocalBinder extends Binder {
        ARFFRecorderService getService() {
            return ARFFRecorderService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (clientActivity != null) clientActivity.updateAccStatus("Callback handler");
            return true;
        }
    });

    public void setClientActivity(ARFFRecorderActivity clientActivity) {
        this.clientActivity = clientActivity;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (clientActivity.disp_rate > 0) {
            timer1 = new Timer();
            timer1.schedule(new TimerTask() {
                @Override
                public void run() {
                    String out_text = "acc  x = " + accx + "  y = " + accy  + "  z = " + accz;
                    arrlist_ts.add(ts);
                    arrlist_x.add(accx);
                    arrlist_y.add(accy);
                    arrlist_z.add(accz);
                    if (clientActivity != null) clientActivity.updateAccStatus(out_text);
                }
            }, 1000, (int) (1000 / clientActivity.disp_rate));
        }

        if (!recording) {
            recording = true;
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(getText(R.string.app_name))
                            .setContentText(getText(R.string.recording));
            Intent resultIntent = new Intent(this, ARFFRecorderActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(ARFFRecorderActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            mNM.notify(NOTIFICATION, mBuilder.build());

            mSensorManager.registerListener(this, mAccelerometer, clientActivity.samp_rate);
            if (clientActivity != null) clientActivity.updateAccStatus("acc started");
        }
        return START_STICKY;
    }

    public void onSensorChanged(SensorEvent e) {
        ts = (int) (new Date().getTime()/1000);
        accx = (int) (100000 * e.values[0]);
        accy = (int) (100000 * e.values[1]);
        accz = (int) (100000 * e.values[2]);
    }

    private void showMessage(String text) {
        Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
        toast.show();
    }

    @Override
    public void onDestroy() {
        stopRecording();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public boolean isRecording() {
        return recording;
    }

    public void stopRecording() {
        if (recording) {
            mSensorManager.unregisterListener(this);
            mNM.cancel(NOTIFICATION);
            recording = false;
            if (timer1 != null) {
                String s_ts = "";
                String s_x = "";
                String s_y = "";
                String s_z = "";
                StringBuilder sb_ts = new StringBuilder();
                for (int i = 0; i < arrlist_ts.size(); i++) {
                    sb_ts.append(arrlist_ts.get(i));
                }
                StringBuilder sb_x = new StringBuilder();
                for (int i = 0; i < arrlist_x.size(); i++) {
                    sb_x.append(arrlist_x.get(i));
                }
                StringBuilder sb_y = new StringBuilder();
                for (int i = 0; i < arrlist_ts.size(); i++) {
                    sb_y.append(arrlist_y.get(i));
                }
                StringBuilder sb_z = new StringBuilder();
                for (int i = 0; i < arrlist_ts.size(); i++) {
                    sb_z.append(arrlist_z.get(i));
                }
                s_ts = sb_ts.toString();
                s_x = sb_x.toString();
                s_y = sb_y.toString();
                s_z = sb_z.toString();
                timer1.cancel();
            }
            if (clientActivity != null) clientActivity.updateAccStatus("acc stopped");
            stopSelf();
        }
    }

}

here you have some DOC with all options for storing data, maybe SharedPreferences would be better/easier to implement?

if you really need a file on storage/sd card then check out THIS SO question and answers, there is also an official DOC section about this way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM