0

我正在使用 IntentService 将单个 NMEA 字符串附加到存储在 Android 设备上的外部公共存储中的日志文件中,但我的行为不一致。

首先,连接设备进行 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