Search code examples
androiddelphifiremonkey

How to restart an app programmatically for android?


First of all, I know a little bit about restarting an app. but that's when it is for windows. In this case, I need to make this for an app that is from android. I couldn't find a solution for it that works in Delphi. Just found this from @Mihai Limbășan I quote:

Perhaps you should think outside the box. Instead of futzing with the mutex/instance logic, you could simply create another executable that waits for your app to close then starts it again. As an added bonus, you can later use this mechanism to, for example, update some of your main app's binaries. It's also much easier to run it elevated instead of maintaining different integrity levels inside the same app, etc.

But have no idea how this works or even where to start... Every tip, code sample, or maybe other solution to restart an app will be appreciated.

EDIT

after some questions here are some pieces of code from the procedure.

First. after you choose for example the language 'English and push the button save this happens

  Inifile := TIniFile.Create(fPath);
  try
  Inifile.WriteString('Instelling','ip',edit5.text);
  Inifile.WriteString('Instelling','user',edit6.text);
  Inifile.WriteString('Instelling','pixels',edit3.text);
  Inifile.WriteInteger('Instelling','language',Combobox2.ItemIndex);
  fGebruiker := Edit6.Text;
  fFotoformaat := StrToInt(edit3.Text);
  finally
  FDConnection1.Params.Values['server']:=edit5.Text;
  FDConnection1.Connected := True;
  inifile.free;
  End;

with this code, I fill an inifile with data as you see also the item index of the Combobox for the language.

on this point i restart the app manually so the right language is chosen by this code:

procedure TfmMain.FormShow(Sender: TObject);
VAR
param : string;
inifile : tInifile;
begin
 


if (System.SysUtils.fileexists(fPath)) then
     Begin

         begin
          Inifile := TIniFile.Create(fPath);

              try
              if not (Inifile.ReadString('Instelling','ip','default')='default') and not (Inifile.ReadString('Instelling','Gebruiker','default')='default')then
                try

                edit5.text := Inifile.ReadString('Instelling','ip','default');
                edit6.text := Inifile.ReadString('Instelling','user','default');
                Edit3.text := Inifile.ReadString('Instelling','pixel','400');
                combobox2.ItemIndex := IniFile.ReadInteger('Instelling','language',1);
                fpixel:= StrToInt(edit3.Text);
                fuser:=edit6.text;
                FDConnection1.Params.Values['server']:=edit5.Text;
                 taal := 'NL';


                //find language settings
                  if combobox2.ItemIndex=0 then
                  begin
                    language:= 'NL'
                  end;

                  if combobox2.ItemIndex=1 then
                  begin
                    language:= 'ENG';
                  end;

                 if language='ENG' then
                 begin
                 vertalerENG.vertaler('ENG');
                 end;
              end;
            end;
         end;
      end;

the VertalerENG is a function that is fired if the language parameter is ENG and change all of the captions to English.

the problem is that nothing is changed till i restart the app.


Solution

  • If you want restart app programmatically, This code work fine for me and you can set time elapse before restart app

    uses
    Androidapi.Helpers,Androidapi.JNI.GraphicsContentViewText,Androidapi.JNI.App,
    System.DateUtils;
    ...
    
    procedure RestartApp;
         {$IFDEF ANDROID}
        var  LPM : JPackageManager;
          LIntent_Start : JIntent;
          LPendingIntent : JPendingIntent;
          LMS : Int64;
         {$ENDIF}
    begin
        {$IFDEF ANDROID}
          LPM := TAndroidHelper.Context.getPackageManager();
          LIntent_Start := LPM.getLaunchIntentForPackage(
              TAndroidHelper.Context.getPackageName()
              );
          LIntent_Start.addFlags( TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP );
        
          LPendingIntent := TJPendingIntent.JavaClass.getActivity(
              TAndroidHelper.Context,
              223344 {RequestCode},
              LIntent_Start,
              TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
              );
          LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
        
          TAndroidHelper.AlarmManager.&set(
              TJAlarmManager.JavaClass.RTC,
              LMS + 10000,
              LPendingIntent
              );
              // TAndroidHelper.Activity.finish();
            Application.Terminate;
        {$ENDIF }
    end;