简体   繁体   English

Android 应用程序 - 将传感器数据保存到本地文件

[英]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.我正在做一个项目来记录来自加速度计传感器 (xyz) 的数据并将其保存到我手机上的文件中。 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.不幸的是,我根本不知道 Java,所以我希望这里有人提示添加什么来实现保存部分。 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.我假设我需要不断地将来自 onSensorChanged(变量 accx、accy、accz)的变量附加到一个对象,然后在我停止录制时保存它。

EDIT: I declared the following array lists.编辑:我声明了以下数组列表。 3 for my sensor data and one to write the corresponding timestamps. 3 个用于我的传感器数据,一个用于写入相应的时间戳。

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.我将 add-to-array 命令放在 run() 函数中,紧跟在变量 String out_text 之后。

I then put the string concatenation in the function stopRecording() in the if (timer1 != null) condition.然后我将字符串连接放在函数 stopRecording() 中的 if (timer1 != null) 条件中。 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?在这里,您有一些包含用于存储数据的所有选项的DOC ,也许SharedPreferences会更好/更容易实现?

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如果您真的需要存储/sd 卡上的文件,请查看这个问题和答案,还有关于这种方式的官方DOC部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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