gpt4 book ai didi

Android GPS 在多行 SD 卡内的文本文件中启用/禁用时间保存

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:19 24 4
gpt4 key购买 nike

1.我正在使用 SD 卡内的文本日志文件保存 Android GPS 启用和禁用时间。

我想打印多行文本,而不删除上一行数据。

下面的代码完美地打印了多行,但是当应用程序保持 sleep 并重新启动时。当 GPS 打开或关闭时,打印一行以及以前的数据计数。

帮我解决一下。

Required Output:(after Restart the app)

<< GPs Enabled At :2016/06/08 17.15.00>>
<< GPs Disabled At :2016/06/08 17.45.00>>
<< GPs Enabled At :2016/06/08 17.49.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>

Getting Output:(after Restart the app)

<< GPs Enabled At :2016/06/08 17.15.00>>
<< GPs Disabled At :2016/06/08 17.45.00>>
<< GPs Enabled At :2016/06/08 17.49.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>

日志文件的方法:

 public void Logmethod(String NoramalStr,String Dateval)
{

/* Log FOlder Text files Starts here*/
try {

File newFolder = new File(Environment.getExternalStorageDirectory(), "GpsFolder");
if (!newFolder.exists()) {
newFolder.mkdir();
}
try {
File file = new File(newFolder, "GPSStatus" + ".txt");
file.createNewFile();

/*Writing the Log in specific files*/
BufferedWriter out;
try {


String separator1 = System.getProperty("line.separator");
FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPSStatus.txt"), true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.append("<< " + NoramalStr +" : "+Dateval + " >>");
osw.append(separator1); // this will add new line ;
osw.flush();
osw.close();
fOut.close();

Log.i(NoramalStr," : "+Dateval);

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


} catch (Exception ex) {
System.out.println("ex: " + ex);
}
} catch (Exception e) {
System.out.println("e: " + e);
}


}

最佳答案

如果是关于删除日志文件的问题:你总是做 createNewFile 尝试检查文件是否存在

public void Logmethod(String NoramalStr, String Dateval) {

try {

File newFolder = new File(Environment.getExternalStorageDirectory(), "GpsFolder");
boolean directoryExists = newFolder.exists();

Log.d(TAG, " directoryExists = " + directoryExists);

if (!directoryExists) {
boolean dirCreate = newFolder.mkdir();
Log.d(TAG, " dirCreate = " + dirCreate);
}
File file = new File(newFolder, "GPSStatus" + ".txt");

boolean fileExists = file.exists();
Log.d(TAG, " fileExists = " + fileExists);

if (!fileExists) {
boolean fileCreateResult = file.createNewFile();
Log.d(TAG, " fileCreateResult = " + fileCreateResult);
}

String separator1 = System.getProperty("line.separator");
FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPSStatus.txt"), true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.append("<< ");
osw.append(NoramalStr);
osw.append(" : ");
osw.append(Dateval);
osw.append(" >>");
osw.append(separator1); // this will add new line ;
osw.flush();
osw.close();
fOut.close();

Log.i(TAG, " : " + Dateval);

} catch (Exception e) {
e.printStackTrace();
}
}

如果是关于永久监听位置提供者启用/禁用How do I receive the system broadcast when GPS status has changed?并在 list 文件中使用接收器

    <receiver android:name=".GPStStatusReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>

关于Android GPS 在多行 SD 卡内的文本文件中启用/禁用时间保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34875423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com