0

asynctaskを使用してファイルを作成しました。その後、エグゼキュータが1秒に1回そのファイルに情報を書き込むようにスケジュールしました。ボタンに触れると、エグゼキュータがシャットダウンされ、ファイルが閉じられますが、多くの場合、ファイルには何も書き込まれません。

コード:

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);
                }
        });

停止ボタンが押されるたびに、以前に照会されたSharedPreferenceがtrueに設定されます。

4

1 に答える 1

0

あなたの問題はここにあるのではないかと思います

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

new File(fileName)このタスクでファイルが作成され、可能な場合はファイルがすでに作成されていると想定しています。これが本当かどうかは定かではありません。AsyncTask CreateFileTaskの実行がスケジュールされ、次のステートメントが実行される前に完了する場合、ファイルはそこにあります。それ以外の場合はありません。IOExceptionまたはFileNoteFoundExceptionsからlogcatにスタックトレースが表示されていますか?

于 2012-08-21T16:34:09.200 に答える