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

前端之家收集整理的这篇文章主要介绍了Android GPS启用/禁用时间保存在带有多条线的SDCARD内的文本文件中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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>>

日志文件方法

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?
并在清单文件中使用接收器

<receiver android:name=".GPStStatusReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>
原文链接:https://www.f2er.com/android/315109.html

猜你在找的Android相关文章