-1

我正在处理以下代码,我查看了其他问题,例如 AsyncTask Android 示例如何正确使用 AsyncTask ,但我仍然遇到问题,请告诉我我缺少什么?非常感谢!!!

因此,在 UI 中,我有一个开始按钮、一个停止按钮和一个文本视图。我从加速度计传感器获取数据并存储在 csv 文件中。不使用 AsyncTask,代码可以正常工作,但是使用它时(如下所示),我收到此错误:a_datatest 无法解析为变量

所以,基本上我想要的是在单击开始按钮后立即开始在后台写入加速度计数据,然后一旦单击停止按钮,文本视图就会显示“数据已存储!”

我很感激任何帮助!

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener, OnClickListener {

    private SensorManager sensorManager;
    private LocationManager locationManager;
    private Button start_bt;
    private Button stop_bt;
    private TextView result_view;
    private boolean started = false;
    String towers;    
    FileOutputStream fos;
    String FILENAME = "AccelOutput";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria crit = new Criteria();
        towers = locationManager.getBestProvider(crit, false);
        start_bt = (Button) findViewById(R.id.startButton);
        stop_bt = (Button) findViewById(R.id.stopButton);
        result_view = (TextView) findViewById(R.id.resultTextView);
        start_bt.setOnClickListener(this);
        stop_bt.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (started) {
            Sensor sensortype = event.sensor;

            if (sensortype.getType() == Sensor.TYPE_ACCELEROMETER) {

                double a_x = event.values[0];
                double a_y = event.values[1];
                double a_z = event.values[2];
                double a_a = event.accuracy;
                double a_ts = event.timestamp;
                long a_timestamp = System.currentTimeMillis();
                AccelData a_data = new AccelData(a_timestamp, a_ts, a_x, a_y, a_z, a_a);                
                String a_datatest = a_data.toString();

                new writeData().execute();
            }       
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.startButton) {
            started = true;
            Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_FASTEST);
        } else if(v.getId() == R.id.stopButton) {
            started = false;
            sensorManager.unregisterListener(this);
        }
    }

    private class writeData extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File (sdCard.getAbsolutePath() + "/output");
            directory.mkdirs();

            String a_filename = "accelerometer.csv";
            File a_file = new File(directory, a_filename);
            FileOutputStream a_fOut = null;
            try {
                a_fOut = new FileOutputStream(a_file, true);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            OutputStreamWriter a_osw = new OutputStreamWriter(a_fOut);
            try {
                a_osw.write(a_datatest + "\n");
                a_osw.flush();
                a_osw.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
              result_view.setText("Data Stored!"); 
        }
    }
}
4

2 回答 2

1

在类范围内声明变量。即用它作为成员变量

a_datatestonSensorChanged方法中声明,因此它在此范围之外没有有效性。所以你不能在doInBackGround方法中使用它

班级内的delcare

private String a_datatest;

在 onSensorChanged 方法中使用类似

a_datatest = a_data.toString();
于 2013-06-20T16:52:42.607 回答
0

我认为这不是AsyncTask实现这一目标的最佳方式。AsyncTask设计用于一次性操作,例如从服务器下载一组数据。启动,在AsyncTask后台执行某些操作,然后向 UI 线程报告。虽然可以停止AsyncTask它,但它不能按您想要的方式工作。

我建议只使用 a Service。您可以通过按下按钮开始读取值,然后停止Service停止按钮的 onClick。

于 2013-06-20T16:56:23.907 回答