[FireMonkey + Android]重启 App

参考来源:

https://forums.embarcadero.com/thread.jspa?threadID=252411

首先,引用单元:

uses
  System.DateUtils,
  Androidapi.JNI.GraphicsContentViewText,
  FMX.Platform.Android,
  Androidapi.Helpers,
  Androidapi.JNI.App

详细代码:

procedure ReStartApp();
var
  LPM : JPackageManager;
  LIntent_Start : JIntent;
  LPendingIntent : JPendingIntent;
  LMS : Int64;
begin
  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
      );

// System.currentTimeMillis() is "wall clock time in UTC".
//  LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000; //slow
  LMS := LMS + System.DateUtils.MilliSecondOf( Now ); //Fast

  TAndroidHelper.AlarmManager.&set(
      TJAlarmManager.JavaClass.RTC,
      LMS + 500,
      LPendingIntent
      );

  TAndroidHelper.Activity.finish();
end;

调用方法:

ReStartApp();

你可能感兴趣的:([FireMonkey + Android]重启 App)