2016-01-19 25 views
7

1.Imp Salvataggio del GPS Android abilitare e disabilitare il tempo utilizzando un file di registro di testo all'interno della scheda SD.Abilita/disabilita GPS Android Abilita/disabilita tempo in un file di testo all'interno di SDCARD con linee a più righe

Desidero stampare più righe di testo, senza cancellare i dati della riga precedente.

Il codice riportato di seguito è la stampa di più righe perfettamente ma quando l'app viene tenuta in sospensione e riavvio. Stampa di una riga insieme al conteggio dei dati precedenti, quando il GPS è attivo o spento.

Aiutami a risolverlo.

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>> 

metodo per file di registro:

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


     } 
+2

io sono un po 'poco chiaro sulla questione. Si menziona la parola "cancella" più volte, ma sembra che la differenza tra l'output richiesto e l'output effettivo sia la stampa di linee duplicate. Il problema con le duplicazioni o con le cancellature? – OYRM

+0

Sembra che il Logmetode sia chiamato più volte. Puoi aggiungere il codice che chiama quel metodo? – TDG

+0

@Kumar: stai provando a stampare i log quando l'app non è in esecuzione o riavviare il dispositivo? –

risposta

0

Se la domanda circa cancellature di file di log: fai sempre createNewFile prova per verificare la presenza di file esiste

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

Se riguarda l'ascolto permanente dei provider di localizzazione abilitare/disabilitare sguardo How do I receive the system broadcast when GPS status has changed? ricevitore e l'uso nel file manifesto

<receiver android:name=".GPStStatusReceiver"> 
     <intent-filter> 
      <action android:name="android.location.PROVIDERS_CHANGED" /> 
     </intent-filter> 
    </receiver> 
0
try { 
      File file = new File(newFolder, "GPSStatus" + ".txt"); 
       if (!file.exists()) { 
      file.createNewFile(); 
     } 

e poi

 FileWriter writer = new FileWriter(file,true); 
     writer.append(response);// you want to expand 
     writer.flush(); 
     writer.close(); 
     Log.e("Suceess", "'file Write"); 

Questo aiuto si