Android实例-IdHTTP下载(并实现自动安装)(XE10+小米2)
相关资料:
1.群号 383675978
2.运行时提示"connection closed gracefully"错误原因与解决 http://www.delphifans.com/InfoView/Article_4588.html
3.编译时提示"[Exec Error] The command ..... exited with code 1."错误原因不明,解决方法删除androidSDK,下载新版本的,引入新版本的SDK。
实例源码:
1 unit Unit1;
2
3 interface
4
5 uses
6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
8 FMX.Controls.Presentation, FMX.StdCtrls, FMX.Objects,
9 System.IOUtils, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
10 Androidapi.Jni.Net,//TJnet_Uri使用
11 Androidapi.JNI.JavaTypes, //Jfile使用
12 Androidapi.JNI.GraphicsContentViewText,//JIntent使用
13 Androidapi.Helpers,//stringtojstring使用
14 IdHTTP;//TPath使用
15
16 type
17 TForm1 = class(TForm)
18 Label1: TLabel;
19 ProgressBar1: TProgressBar;
20 Label2: TLabel;
21 Button1: TButton;
22 IdHTTP1: TIdHTTP;
23 procedure Button1Click(Sender: TObject);
24 procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
25 AWorkCount: Int64);
26 procedure IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
27 AWorkCountMax: Int64);
28 procedure IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
29 private
30 { Private declarations }
31 isDown:Boolean;
32 function DownFilePath: string;
33 public
34 { Public declarations }
35 end;
36
37 const
38 FileName = 'test.apk';
39
40 var
41 Form1: TForm1;
42
43 implementation
44
45 {$R *.fmx}
46 {$R *.NmXhdpiPh.fmx ANDROID}
47
48 procedure TForm1.Button1Click(Sender: TObject);
49 var
50 sURLPath,sFilePath: string;
51 oFile: TFileStream;
52 begin
53 isDown := True;
54 sFilePath := DownFilePath + FileName;
55 if FileExists(sFilePath) then //判断以前文件是否存在
56 DeleteFile(sFilePath);
57
58 // sURLPath:= 'http://bcscdn.baidu.com/netdisk/BaiduYun_7.12.1.apk';//文件下载地址(用百度云测试了)
59 // 本来想用百度云的,我艹,不知道为什么百度云下载着,就提示“connection closed gracefully”
60 sURLPath:= 'http://m.down.sandai.net/MobileThunder/Android_5.15.2.3820/XLWXguanwang.apk';//文件下载地址(用百度云测试了)
61
62 oFile := TFileStream.Create(sFilePath, fmCreate); //文件下载后保存的路径
63 try
64 ProgressBar1.Value := 0;
65 IdHTTP1.Get(sURLPath, oFile);
66 except
67 end;
68 oFile.Free;
69 end;
70
71 function TForm1.DownFilePath: string;
72 begin
73 Result := TPath.GetDownloadsPath + PathDelim;
74 end;
75
76 procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
77 AWorkCount: Int64);
78 begin
79 ProgressBar1.Value := AWorkCount;
80 end;
81
82 procedure TForm1.IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
83 AWorkCountMax: Int64);
84 begin
85 ProgressBar1.Value := AWorkCountMax;
86 end;
87
88 procedure TForm1.IdHTTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
89 {$IFDEF ANDROID}
90 Var
91 oFile: Jfile;
92 oIntent: JIntent;
93 {$ENDIF}
94 begin
95 {$IFDEF ANDROID}
96 //下载完成后自动安装
97 if isDown then
98 begin
99 Try
100 oFile := TJfile.JavaClass.init(stringtojstring(DownFilePath), stringtojstring(FileName));
101 oIntent := TJIntent.Create;
102 oIntent.setAction(TJIntent.JavaClass.ACTION_VIEW);
103 oIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
104 oIntent.setDataAndType(TJnet_Uri.JavaClass.fromFile(oFile), StringToJString('application/vnd.android.package-archive'));
105 SharedActivityContext.startActivity(oIntent);
106 Except
107 End;
108 end;
109 {$ENDIF}
110 end;
111
112 end.
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我