Delphi 10.2 Tokyo Android以编程方式安装.apk(新版本)

问题描述 投票:0回答:1

如何从网址下载应用程序,并使用新版本进行安装

我使用这段代码,但是我收到错误“android.content.ActivityNotFoundException .....”

procedure TfLogin.Button1Click(Sender: TObject);
var
 Intent: JIntent;
begin
  Intent  := TJIntent.Create;
  Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.putExtra(StringToJString('EXTRA_NOT_UNKNOWN_SOURCE'), true);
  intent.setDataAndType(TJnet_Uri.JavaClass.parse(StringToJString('https://examples.com/xxx.apk')),
      StringToJString('application/vnd.android.package-archive'));
 Intent.addFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
 SharedActivity.startActivity(Intent);
end;

我得到的错误enter image description here

android delphi firemonkey delphi-10.2-tokyo
1个回答
1
投票
const
  CPackageRootPath = 'http://example.com/temp/';
  CPackageFileName = 'xxxx.apk'; 
  .....

procedure TfLogin.Button1Click(Sender: TObject);
var
  Intent: JIntent;
  URL: string;
  FileName: string;
  Path: JString;
  Name: JString;
  F: JFile;
begin
  ShowMessage('Downloading...');
  URL := Concat(CPackageRootPath, '/', CPackageFileName);
  FileName := TPath.Combine(TPath.GetSharedDownloadsPath(), CPackageFileName);
  if (not DownloadFile(URL, FileName)) then
  begin
    ShowMessage('Download failed');
  end
  else
  begin
    if (TFile.Exists(FileName)) then
    begin
      ShowMessage('File exists');
      Path := StringToJString(TPath.GetDirectoryName(FileName));
      Name := StringToJString(TPath.GetFileName(FileName));
      F := TJfile.JavaClass.init(Path, Name);
      ShowMessage(Format('  Length: %d', [F.length]));
    end;
  end;
  ShowMessage('Done');

  StartActivity(FileName);
end;

procedure TfLogin.StartActivity(const FileName: string);
var
  Path: JString;
  Name: JString;
  F: Jfile;
  Intent: JIntent;
begin
  Path := StringToJString(TPath.GetDirectoryName(FileName));
  Name := StringToJString(TPath.GetFileName(FileName));
  F := TJfile.JavaClass.init(Path, Name);

  Intent := TJIntent.Create();
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
  Intent.setDataAndType(TJnet_Uri.JavaClass.fromFile(F), StringToJString('application/vnd.android.package-archive'));

  TAndroidHelper.Context.startActivity(Intent);
  Application.Terminate();
end;

function TfLogin.DownloadFile(const URL, FileName: string): Boolean;
var
  Buffer: TMemoryStream;
  Client: THTTPClient;
begin
  if (TFile.Exists(FileName)) then
    TFile.Delete(FileName);

  Buffer := TMemoryStream.Create();
  try
    Client := THTTPClient.Create();
    try
      Client.Get(URL, Buffer);
      Result := Buffer.Size > 0;
      if (Result) then
        Buffer.SaveToFile(FileName);
    finally
      Client.Free();
    end;
  finally
    Buffer.Free();
  end;
© www.soinside.com 2019 - 2024. All rights reserved.