0

Android デバイスの外部パブリック ストレージに保存されているログ ファイルに単一の NMEA 文字列を追加するために IntentService を使用していますが、動作に一貫性がありません。

まず、デバイスが USB デバッグ用に接続されている場合、ログ ファイルは表示されません。多くの Android デバイスは、USB 経由で接続すると外部ストレージに書き込めないと読みましたが、USB から切断して実行した場合でも、ログ ファイルがファイル システムに表示されるまでに、電源のオン/オフとアプリケーションの再実行を数回行う場合があります。 . ログを消去しない場合でも、ファイルへの追加を再開する前に電話を再起動する必要があります。

ファイルが常に一貫して表示されるようにするにはどうすればよいですか?

import android.app.IntentService;
import android.content.Intent;


public class LogFileService extends IntentService {

    DebugLog debug = new DebugLog("LogFileService");

    public static final String GPS_STR = "GPS_STR";
    public static final String FILE_PATH = "FILE_PATH";
    public static final String FILE_NAME = "gps_data.txt";




    public LogFileService() {
        super("LogFileService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {


        debug.log("Handling intent");

        String data = intent.getStringExtra(GPS_STR);

        debug.log("Writing " + data);

        GpsLogFile logFile = new GpsLogFile(FILE_NAME);
        logFile.open();
        logFile.write(data);
        logFile.close();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        debug.log("Created");
    }


}

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import android.content.Context;
import android.os.Environment;

public class GpsLogFile {

    DebugLog debug = new DebugLog("LogFile");


    private File filePath = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS
    );

    private File logFile = null;
    private String fileName = null;


    private PrintWriter fileOut = null;

    public GpsLogFile(String name) {
        fileName = name;
    }

    public void open() {

        try {

            filePath.mkdirs();  

            if (!isExternalMediaAvailable()) {
                throw new IOException("External media not available.");
            }
            logFile = new File(filePath, fileName);
            //logFile.mkdirs();
            if (!logFile.exists()) {
                logFile.createNewFile();
            }
            fileOut = new PrintWriter(
                    new FileOutputStream(logFile.getAbsolutePath(), true)
            );

        } catch (IOException e) {
            debug.log("Unable to open file.");
            debug.log(e.getMessage());
        }

    }

    public void write(String data) {

        debug.log("Writing to " + logFile.getAbsolutePath() + " " + data);

        try {
            fileOut.write(data);
            //fileOut.newLine();
            checkPrintWriterError(fileOut);
            fileOut.flush();

        } catch (IOException e) {
            debug.log("Unable to write");
            debug.log(e.getMessage());
        }
    }

    public void close() {
        if (null != fileOut) {
            try {
                fileOut.close();
                checkPrintWriterError(fileOut);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private void checkPrintWriterError(PrintWriter writer) throws IOException {
        if (true == writer.checkError()) {
            throw new IOException("Print writer error.");
        }
    }
}
4

0 に答える 0