Android執行個體-IdHTTP下載(並實現自動安裝)

來源:互聯網
上載者:User

標籤:

 

相關資料:

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     Try100       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     Except107     End;108   end;109   {$ENDIF}110 end;111 112 end.

 

Android執行個體-IdHTTP下載(並實現自動安裝)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.