Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中

Kum*_*mar 7 gps android

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

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

下面的代码是完美打印多行,但当应用程序保持睡眠和重新启动时.当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>>
Run Code Online (Sandbox Code Playgroud)

日志文件的方法:

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


        }
Run Code Online (Sandbox Code Playgroud)

And*_*ICE 0

如果有关删除日志文件的问题:您总是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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果是关于位置提供商的永久监听启用/禁用, 请参阅当 GPS 状态发生变化时如何接收系统广播? 并在清单文件中使用接收器

    <receiver android:name=".GPStStatusReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)