Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Desarrollo en Delphi para Android (https://www.clubdelphi.com/foros/forumdisplay.php?f=57)
-   -   ¿Cómo guardar el estado de un Tswitch del modo más simple? (https://www.clubdelphi.com/foros/showthread.php?t=88899)

cocute 22-08-2015 10:08:37

¿Cómo guardar el estado de un Tswitch del modo más simple?
 
Pues eso quiero guardar el estado ischecked de un Tswitch, para que se guarde si quiero sonido en la aplicación o no.
No se si me he liado mucho pero esto es lo que intento y no me lo guarda.
Lo que hago es crear un fichero sonido.ini, y luego comprobar si existe ese fichero,
y si existe el fichero que no suene. Pero no me funciona.
En lo de permisos tengo marcado "write in external extorage", no se si necesito algo más.
O si hay algún modo mejor de hacerlo¿?

Por defecto el TSwitch está checked.

Esto pongo en el onshow del form
Código Delphi [-]
procedure TForm.FormShow(Sender: TObject);
begin
if FileExists( TPath.GetDocumentsPath + PathDelim + 'sonido.ini' ) then swtchchk1.IsChecked:=false;

if not FileExists(TPath.GetDocumentsPath + PathDelim + 'sonido.ini') then
       begin
  MP1.FileName := TPath.GetDocumentsPath + PathDelim + 'sonido.mp3';
  MP1.Play;
       end;
end;

y esto en el switch
Código Delphi [-]
procedure TForm.swtchchk1Switch(Sender: TObject);
begin
if swtchchk1.IsChecked=true then
begin
   if FileExists( TPath.GetDocumentsPath + PathDelim + 'sonido.ini' )
   then
       begin
        TFile.Delete( TPath.GetDocumentsPath + PathDelim + 'sonido.ini' );
        mp1.play;
    end;
end;
if swtchchk1.IsChecked=false then
begin
TFile.Create( TPath.GetDocumentsPath + PathDelim + 'sonido.ini' );
mp1.Stop;
end;

end;

cocute 24-08-2015 16:32:31

nada que no hay manera de que me guarde y recupere configuraciones, ni usando ficheros ini tampoco
Que es lo que hago mal?
en este caso intento guardar en un ini el valor de un trackbar "tr1", pero no hay manera tampoco.

Código Delphi [-]
procedure TForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
 var
   Ini: TIniFile;
 begin
   Ini := TIniFile.Create(TPath.GetDocumentsPath + PathDelim + 'SAVE.INI' ) ;
   try
     Ini.WriteFloat('tr1', 'Value', tr1.Value);
   finally
     Ini.Free;
   end;

end;

Código Delphi [-]
procedure TForm.FormCreate(Sender: TObject);

 var
   Ini: TIniFile;
 begin
if FileExists( TPath.GetDocumentsPath + PathDelim + 'SAVE.INI' ) then
 begin

   Ini := TIniFile.Create(TPath.GetDocumentsPath + PathDelim + 'SAVE.INI' ) ;
   try
     Ini.readfloat('tr1', 'Value', tr1.Value);
   finally
     Ini.Free;
   end;

 end;

dec 24-08-2015 16:40:11

Hola,

¿Estás seguro de que "GetDocumentsPath" es la ruta adecuada para usar en aplicaciones Android? Primero el archivo debe existir, después has de tener permisos de lectura y escritura sobre el mismo. Yo creo que por ahí van los tiros, puesto que el código que muestras no contiene nada raro y debería funcionar si lo anterior se cumpliese. Infórmate sobre qué rutas has de usar para guardar archivos de configuración para tu aplicación. Y si ya estás usando la correcta... entonces me temo que no se me ocurre otra cosa sino tratar de depurar lo mejor posible lo que haces ahora.

cocute 24-08-2015 17:22:43

pues no se no hay manera,
igual si meto en el deployment el fichero ini me funcione?
pero que carpeta le pongo en el valor Remote Path que me deje modificar el archivo?

cocute 24-08-2015 20:14:19

Al final creo que lo he solucionado, si usar inis, guardando datos en un memo invisible
he guardado el valor del volumen del sonido de un trackbar
y si quieres sonido o no de un switch

Código Delphi [-]
procedure TForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
 begin
mmo1.Lines.Strings[1]:=FloatToStr(tr1.Value);
if swtchchk1.IsChecked=True then  mmo1.Lines.Strings[0]:='1';
if swtchchk1.IsChecked=false then  mmo1.Lines.Strings[0]:='0';

mmo1.Lines.savetoFile(TPath.Combine(TPath.GetDocumentsPath, 'SAVE.INI' )) ;


end;

procedure TForm.FormCreate(Sender: TObject);

 begin
if FileExists( TPath.Combine(TPath.GetDocumentsPath, 'SAVE.INI')) then
 begin
mmo1.Lines.LoadFromFile(TPath.Combine(TPath.GetDocumentsPath, 'SAVE.INI' )) ;
tr1.Value:=StrTofloat(mmo1.Lines.Strings[1]);
if mmo1.Lines.Strings[0]='1' then swtchchk1.IsChecked:=True;
if mmo1.Lines.Strings[0]='0' then swtchchk1.IsChecked:=false;

end;


La franja horaria es GMT +2. Ahora son las 17:35:12.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi