【问题标题】:How to restart an app programmatically for android?如何以编程方式为android重新启动应用程序?
【发布时间】:2021-12-07 07:41:44
【问题描述】:

首先,我对重启应用有所了解。但那是适用于窗户的时候。在这种情况下,我需要为来自 android 的应用程序制作这个。我找不到适用于 Delphi 的解决方案。刚刚从@Mihai Limbăşan 找到这个我引用:

也许您应该跳出框框思考。而不是与 互斥体/实例逻辑,您可以简单地创建另一个可执行文件 等待您的应用程序关闭然后再次启动它。作为额外的奖励, 您可以稍后使用此机制来更新您的一些 主应用程序的二进制文件。提升运行它也容易得多 在同一个应用程序中维护不同的完整性级别等。

但不知道这是如何工作的,甚至不知道从哪里开始...... 我们将不胜感激每一个提示、代码示例或其他重启应用的解决方案。

编辑

这里的一些问题是程序中的一些代码。

首先。 在您选择例如语言“英语”并按下按钮保存后,会发生这种情况

  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;

使用此代码,我用数据填充了一个 inifile,您还可以看到该语言的 Combobox 的项目索引。

在这一点上,我手动重新启动应用程序,以便通过此代码选择正确的语言:

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;

VertalerENG 是一个函数,如果语言参数为 ENG 并将所有字幕更改为英语,则会触发该函数。

问题是在我重新启动应用程序之前没有任何改变。

【问题讨论】:

  • 您能解释一下为什么需要在 Android 中重新启动应用程序吗?如果你想简单地在App中重新加载一些东西,你不能不重新启动就重新加载它吗?
  • @LynchChen 用户可以在语言之间进行选择,当语言发生更改时,我想重新启动应用程序,以便按钮具有正确的标题
  • 这是不正确的,您不应该重新启动应用程序,您应该使用myButton.setText() 之类的方法更新UI,并在更改语言时调用它。在 Android 中更改语言而重新启动并不常见,这不是 Windows。
  • @LynchChen 如果您从更改事件的组合框中选择语言,我会触发一个函数,其中所有按钮将其文本更改为所选语言但没有任何更改,当我重新启动应用程序时然后它改变了。这就是为什么我认为这会有所帮助。
  • @DalijaPrasnikar 谢谢!效果很好!

标签: android delphi firemonkey


【解决方案1】:

如果您想以编程方式重新启动应用程序, 这段代码对我来说很好,你可以在重启应用程序之前设置时间过去

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;

【讨论】:

    【解决方案2】:

    如果更改语言是您唯一关心的问题,那么我建议更改应用程序的语言环境。如果您正确使用了strings.xml中的所有字符串,则只需重新启动活动

    您可以在此处查看如何以编程方式更改应用程序的语言环境。

    Change app language programmatically in Android

    【讨论】:

    • 这是 Delphi Android 应用程序,其工作方式与标准 Android 应用程序截然不同。这个答案甚至可以远程回答这个问题。
    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    相关资源
    最近更新 更多