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;