安卓APP在线升级

安卓APP在线升级

通过IDHTTP组件在线下载APP到手机中,然后自动安装这个APP程序。

1)在线下载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;

  

2)安装下载的APK

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;

  

posted @ 2019-05-22 21:03  delphi中间件  阅读(1307)  评论(0编辑  收藏  举报