Delphi XE8 利用HttpClient实现的一个App自动更新组件
2015-06-01 14:00
741 查看
第一个版本,分享了。
unit AutoUpdate;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, System.IOUtils, System.JSON, System.Net.HttpClient
{$IFDEF MSWINDOWS}
, Winapi.Windows,
{$ENDIF}
{$IFDEF IOS}
, FMX.Platform.iOS,
iOSapi.Foundation,
Macapi.ObjectiveC,
{$ELSE}
{$IFDEF MACOS}
, FMX.Platform.Mac,
Macapi.Foundation,
Macapi.ObjectiveC,
{$ELSE}
{$IFDEF ANDROID}
, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android, Androidapi.Helpers,
Androidapi.JNI.Net,
{$ENDIF ANDROID}
{$ENDIF MACOS}
{$ENDIF IOS}
FMX.Dialogs;
type
TAutoUpdate = class(TComponent)
private
FServerVersion, FInstallFileUrl, FDescription: String;
FInstallFileName: string;
FCheckURL: String;
procedure SetCheckURL(const Value: String);
public
constructor Create(AOwner: TComponent); override;
function Check: Boolean;
procedure DownLoad;
procedure Install;
property CheckURL: String read FCheckURL write SetCheckURL;
property ServerVersion: string read FServerVersion;
property Description: String read FDescription;
end;
function GetAppVersion: String;
implementation
function GetAppVersion: String;
{$IFDEF MSWINDOWS}
function GetBuildInfoAsString: string;
var
V1, V2, V3, V4: word;
procedure GetBuildInfo(var V1, V2, V3, V4: word);
var
VerInfoSize, VerValueSize, Dummy: DWORD;
VerInfo: Pointer;
VerValue: PVSFixedFileInfo;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
if VerInfoSize > 0 then
begin
GetMem(VerInfo, VerInfoSize);
try
if GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo)
then
begin
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
V1 := dwFileVersionMS shr 16;
V2 := dwFileVersionMS and $FFFF;
V3 := dwFileVersionLS shr 16;
V4 := dwFileVersionLS and $FFFF;
end;
end;
finally
FreeMem(VerInfo, VerInfoSize);
end;
end;
end;
begin
GetBuildInfo(V1, V2, V3, V4);
result := IntToStr(V1) + '.' + IntToStr(V2) + '.' + IntToStr(V3) + '.' +
IntToStr(V4);
end;
begin
result := GetBuildInfoAsString;
end;
{$ENDIF}
{$IFDEF ANDROID}
var
PackageInfo: JPackageInfo;
PackageName: JString;
begin
PackageName := SharedActivityContext.getPackageName;
PackageInfo := SharedActivityContext.getPackageManager.getPackageInfo
(PackageName, 0);
result := JStringToString(PackageInfo.versionName);
end;
{$ENDIF}
{$IFDEF MACOS}
var
AppNameKey: Pointer;
AppBundle: NSBundle;
NSAppName: NSString;
begin
AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
AppNameKey := (NSSTR('CFBundleName') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleName : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleDisplayName') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleDisplayName : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleIdentifier') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleIdentifier : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleVersion') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleVersion : ' + UTF8ToString(NSAppName.UTF8String));
result := UTF8ToString(NSAppName.UTF8String);
end;
{$ENDIF}
{ TAutoUpdate }
function TAutoUpdate.Check: Boolean;
var
LocalVersion: string;
hc: THTTPClient;
ss: TStringStream;
jo: TJsonObject;
begin
hc := THTTPClient.Create;
ss := TStringStream.Create;
try
hc.Get(FCheckURL, ss);
jo := TJsonObject.ParseJSONValue(ss.DataString) as TJsonObject;
try
FServerVersion := jo.GetValue('Version').Value;
FDescription := jo.GetValue('Description').Value;
FInstallFileUrl := jo.GetValue('URL').Value;
finally
jo.Free;
end;
finally
hc.Free;
ss.Free;
end;
LocalVersion := GetAppVersion;
result := CompareText(FServerVersion, LocalVersion) > 0;
end;
constructor TAutoUpdate.Create(AOwner: TComponent);
begin
inherited;
FInstallFileName := TPath.GetSharedDocumentsPath + PathDelim + 'install.apk';
end;
procedure TAutoUpdate.DownLoad;
var
Stream: TMemoryStream;
hc: THTTPClient;
begin
Stream := TMemoryStream.Create;
hc := THTTPClient.Create;
try
try
hc.Get(FInstallFileUrl, Stream);
Stream.SaveToFile(FInstallFileName);
except
on e: Exception do
ApplicationShowException(e);
end;
finally
hc.Free;
Stream.Free;
end;
end;
procedure TAutoUpdate.Install;
{$IFDEF ANDROID}
var
Intent: JIntent;
aUrl: string;
begin
aUrl := 'file://' + FInstallFileName;
Intent := TJIntent.Create;
Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(TJnet_Uri.JavaClass.parse(StringToJString(aUrl)),
StringToJString('application/vnd.android.package-archive'));
try
SharedActivity.startActivity(Intent);
except
on e: Exception do
begin
// if DisplayError then
ShowMessage('Error: ' + e.Message);
end;
end;
end;
{$ELSE}
begin
end;
{$ENDIF}
procedure TAutoUpdate.SetCheckURL(const Value: String);
begin
FCheckURL := Value;
end;
end.
使用了网友的取版本号及调用apk,这里谢过!
调用方法:
procedure TMainForm.Button1Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(
procedure()
var
au: TAutoUpdate;
begin
au := TAutoUpdate.Create(Self);
try
au.CheckURL := 'http://wx.test.cc/test.inf';
if au.Check then
begin
au.DownLoad;
au.Install;
end;
finally
au.Free;
end;
end).Start;
end;
转自:http://blog.sina.com.cn/s/blog_44fa172f0102vgd1.html
unit AutoUpdate;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, System.IOUtils, System.JSON, System.Net.HttpClient
{$IFDEF MSWINDOWS}
, Winapi.Windows,
{$ENDIF}
{$IFDEF IOS}
, FMX.Platform.iOS,
iOSapi.Foundation,
Macapi.ObjectiveC,
{$ELSE}
{$IFDEF MACOS}
, FMX.Platform.Mac,
Macapi.Foundation,
Macapi.ObjectiveC,
{$ELSE}
{$IFDEF ANDROID}
, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android, Androidapi.Helpers,
Androidapi.JNI.Net,
{$ENDIF ANDROID}
{$ENDIF MACOS}
{$ENDIF IOS}
FMX.Dialogs;
type
TAutoUpdate = class(TComponent)
private
FServerVersion, FInstallFileUrl, FDescription: String;
FInstallFileName: string;
FCheckURL: String;
procedure SetCheckURL(const Value: String);
public
constructor Create(AOwner: TComponent); override;
function Check: Boolean;
procedure DownLoad;
procedure Install;
property CheckURL: String read FCheckURL write SetCheckURL;
property ServerVersion: string read FServerVersion;
property Description: String read FDescription;
end;
function GetAppVersion: String;
implementation
function GetAppVersion: String;
{$IFDEF MSWINDOWS}
function GetBuildInfoAsString: string;
var
V1, V2, V3, V4: word;
procedure GetBuildInfo(var V1, V2, V3, V4: word);
var
VerInfoSize, VerValueSize, Dummy: DWORD;
VerInfo: Pointer;
VerValue: PVSFixedFileInfo;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
if VerInfoSize > 0 then
begin
GetMem(VerInfo, VerInfoSize);
try
if GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo)
then
begin
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
V1 := dwFileVersionMS shr 16;
V2 := dwFileVersionMS and $FFFF;
V3 := dwFileVersionLS shr 16;
V4 := dwFileVersionLS and $FFFF;
end;
end;
finally
FreeMem(VerInfo, VerInfoSize);
end;
end;
end;
begin
GetBuildInfo(V1, V2, V3, V4);
result := IntToStr(V1) + '.' + IntToStr(V2) + '.' + IntToStr(V3) + '.' +
IntToStr(V4);
end;
begin
result := GetBuildInfoAsString;
end;
{$ENDIF}
{$IFDEF ANDROID}
var
PackageInfo: JPackageInfo;
PackageName: JString;
begin
PackageName := SharedActivityContext.getPackageName;
PackageInfo := SharedActivityContext.getPackageManager.getPackageInfo
(PackageName, 0);
result := JStringToString(PackageInfo.versionName);
end;
{$ENDIF}
{$IFDEF MACOS}
var
AppNameKey: Pointer;
AppBundle: NSBundle;
NSAppName: NSString;
begin
AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
AppNameKey := (NSSTR('CFBundleName') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleName : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleDisplayName') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleDisplayName : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleIdentifier') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleIdentifier : ' + UTF8ToString(NSAppName.UTF8String));
AppNameKey := (NSSTR('CFBundleVersion') as ILocalObject).GetObjectID;
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey
(AppNameKey));
// Memo1.Lines.Add('CFBundleVersion : ' + UTF8ToString(NSAppName.UTF8String));
result := UTF8ToString(NSAppName.UTF8String);
end;
{$ENDIF}
{ TAutoUpdate }
function TAutoUpdate.Check: Boolean;
var
LocalVersion: string;
hc: THTTPClient;
ss: TStringStream;
jo: TJsonObject;
begin
hc := THTTPClient.Create;
ss := TStringStream.Create;
try
hc.Get(FCheckURL, ss);
jo := TJsonObject.ParseJSONValue(ss.DataString) as TJsonObject;
try
FServerVersion := jo.GetValue('Version').Value;
FDescription := jo.GetValue('Description').Value;
FInstallFileUrl := jo.GetValue('URL').Value;
finally
jo.Free;
end;
finally
hc.Free;
ss.Free;
end;
LocalVersion := GetAppVersion;
result := CompareText(FServerVersion, LocalVersion) > 0;
end;
constructor TAutoUpdate.Create(AOwner: TComponent);
begin
inherited;
FInstallFileName := TPath.GetSharedDocumentsPath + PathDelim + 'install.apk';
end;
procedure TAutoUpdate.DownLoad;
var
Stream: TMemoryStream;
hc: THTTPClient;
begin
Stream := TMemoryStream.Create;
hc := THTTPClient.Create;
try
try
hc.Get(FInstallFileUrl, Stream);
Stream.SaveToFile(FInstallFileName);
except
on e: Exception do
ApplicationShowException(e);
end;
finally
hc.Free;
Stream.Free;
end;
end;
procedure TAutoUpdate.Install;
{$IFDEF ANDROID}
var
Intent: JIntent;
aUrl: string;
begin
aUrl := 'file://' + FInstallFileName;
Intent := TJIntent.Create;
Intent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(TJnet_Uri.JavaClass.parse(StringToJString(aUrl)),
StringToJString('application/vnd.android.package-archive'));
try
SharedActivity.startActivity(Intent);
except
on e: Exception do
begin
// if DisplayError then
ShowMessage('Error: ' + e.Message);
end;
end;
end;
{$ELSE}
begin
end;
{$ENDIF}
procedure TAutoUpdate.SetCheckURL(const Value: String);
begin
FCheckURL := Value;
end;
end.
使用了网友的取版本号及调用apk,这里谢过!
调用方法:
procedure TMainForm.Button1Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(
procedure()
var
au: TAutoUpdate;
begin
au := TAutoUpdate.Create(Self);
try
au.CheckURL := 'http://wx.test.cc/test.inf';
if au.Check then
begin
au.DownLoad;
au.Install;
end;
finally
au.Free;
end;
end).Start;
end;
转自:http://blog.sina.com.cn/s/blog_44fa172f0102vgd1.html
相关文章推荐
- 浅谈TCP优化
- Alamofire网络库进阶教程
- CentOS 6.4 查看每个进程的网络流量
- 网络工程的学习路程
- linux 网络配置
- http ajp
- 几个常用的异步框架和网络访问框架区分对比
- 简单的网络序列化(反序列化)类
- cocos2d-x网络编程 连接php服务器笔记4
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- TCP/IP TIME_WAIT状态原理
- Linux中TCP连接过程状态简介
- linux配置Httpd授权
- iOS - ASI - ASIHTTPRequest第三方网络请求详解(转载)
- Web服务Https配置和代码访问方法
- HttpWebRequest内存溢出 OutOfMemoryException
- Android---52---使用URl访问网络资源
- 未找到oracle 客户端和网络组件,需要安装客户端8.1.7及以上版本
- cocos2d-x网络编程 连接php服务器笔记3
- tcp有限状态机分析