本文章介绍了Delphi XE7实现的在线更新APP的一个程序,该程序通过IDHTTP组件在线下载APP到手机中,然后自动安装这个APP程序。
程序首先点击DownLoad APP按钮,实现在线下载APP程序
需引用单元:
{$IFDEF ANDROID}
FMX.Helpers.Android, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net, Androidapi.JNI.JavaTypes;
{IFDEF}
procedure TForm1.Button2Click(Sender: TObject);
var
caminho_d: string;
MyFile: TFileStream;
begin
//verifica se ja existe para apagar e criar novamente
if FileExists(GetSharedDownloadsDir+’/PJMOBILE.apk’) then
DeleteFile(GetSharedDownloadsDir+’/PJMOBILE.apk’);
// definir o link
caminho_d:= ‘http://susite.com.br/download/APP.apk‘; // aqui defini um caminho pard鉶 pro download , pode ser trocado por um edit, etc
MyFile := TFileStream.Create(GetSharedDownloadsDir+’/APP.apk’, fmCreate); // local no celular e nome do arquivo com a extens鉶, onde vai salvar.
try
IdHTTP1.Get(caminho_d, MyFile); // fazendo o download do arquivo e salva no celular
finally
MyFile.Free;
end;
end;
procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
AWorkCount: Int64);
begin
ProgressBar1.value := AWorkCount;
Application.ProcessMessages;
end;
procedure TForm1.IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
AWorkCountMax: Int64);
begin
ProgressBar1.value := 0;
ProgressBar1.Max := AWorkCountMax;
Label1.Text := ‘开始下载’;
end;
procedure TForm1.IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
ProgressBar1.Value := ProgressBar1.Max;
Label1.text := ‘下载完成’;
end;
当下在完成后,点击Atualiza APP按钮,ANdroid会提示是否安装该APP,如果安装,那么就进行新的APP的更新覆盖
procedure TForm1.Button1Click(Sender: TObject);
var
aFile:Jfile;
Intent:JIntent;
begin
aFile:=TJfile.JavaClass.init(stringtojstring(GetSharedDownloadsDir),stringtojstring(‘APP.apk’));
Intent := TJIntent.Create ;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
Intent.setDataAndType(TJnet_Uri.JavaClass.fromFile(aFile),StringToJString(‘application/vnd.android.package-archive’));
SharedActivityContext.startActivity(Intent);
end;