Вопрос

I have created a file with an asynctask. Afterwards scheduled an executor to write information to said file once every second. Once I touch a button the executor is shut down and the file closed but more often than not nothing is written in the file.

Code:

startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                preferences.edit().putBoolean(GlobalConstants.stopPreference,false).commit();
                loc_thr = new LocationThread(preferences, getApplicationContext());
                loc_thr.run();

                startButton.setVisibility(View.INVISIBLE);
                startButton.setClickable(false);
                stopButton.setVisibility(View.VISIBLE);
                stopButton.setClickable(true);

                currentDateAndTime = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                fileName = getString(R.string.loc_log_path) + currentDateAndTime + ".txt";
                new CreateFileTask(fileName).execute();
                loc_file = new File(fileName);
                try {
                    FOS = new FileOutputStream(loc_file.getAbsolutePath());
                    OSW = new OutputStreamWriter(FOS);
                    OSW.write(GlobalConstants.fileHeader + '\n');
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                scheduledTaskExecutor = Executors.newScheduledThreadPool(5);
                scheduledTaskExecutor.scheduleAtFixedRate(new Runnable() {
                    public void run() {
                        if(loc_thr.getLocationStatus() & loc_thr.newLocation() & !preferences.getBoolean(GlobalConstants.stopPreference,false)){
                            SensorBundle SB = new SensorBundle(loc_thr.getCurrentLocation(),loc_thr.getCurrentGPSStatus());
                            try {
                                OSW.write(new SimpleDateFormat("yyyy/MM/dd;hh:mm:ss").format(new Date()) + ";");
                                OSW.write(SB.getLatitude() + ";");
                                OSW.write(SB.getLongitude() + ";");
                                OSW.write(SB.getAltitude() + ";");
                                OSW.write(SB.getAccuracy() + ";");
                                OSW.write(SB.getProvider() + ";");
                                OSW.write('\n');
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else{
                            if(preferences.getBoolean(GlobalConstants.stopPreference,false)){
                                try {
                                    OSW.close();
                                    FOS.close();

                                    scheduledTaskExecutor.shutdown();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }, 0, 1, TimeUnit.SECONDS);
                }
        });

Whenever the stop button is pressed the SharedPreference queried earlier is set to true.

Это было полезно?

Решение

I think your problem may be here

   new CreateFileTask(fileName).execute();
   loc_file = new File(fileName);

I assume this task creates the file and you're expecting that when you can new File(fileName) the file is already created. Whether this is true or not is indeterminate. If the AsyncTask CreateFileTask is scheduled to run and completes before the next statement is executed then the file will be there, otherwise it won't be. Are you seeing stack traces in logcat from the IOException or FileNoteFoundExceptions?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top