第一个版本,分享了。
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;
unit AutoUpdate;
interface
uses
{$IFDEF MSWINDOWS}
{$ENDIF}
{$IFDEF IOS}
, FMX.Platform.iOS,
{$ELSE}
{$IFDEF MACOS}
, FMX.Platform.Mac,
{$ELSE}
{$IFDEF ANDROID}
, Androidapi.JNI.GraphicsContentViewText,
{$ENDIF ANDROID}
{$ENDIF MACOS}
{$ENDIF IOS}
type
function GetAppVersion: String;
implementation
function GetAppVersion: String;
{$IFDEF MSWINDOWS}
begin
end;
{$ENDIF}
{$IFDEF ANDROID}
var
begin
end;
{$ENDIF}
{$IFDEF MACOS}
var
begin
end;
{$ENDIF}
{ TAutoUpdate }
function TAutoUpdate.Check: Boolean;
var
begin
end;
constructor TAutoUpdate.Create(AOwner: TComponent);
begin
end;
procedure TAutoUpdate.DownLoad;
var
begin
end;
procedure TAutoUpdate.Install;
{$IFDEF ANDROID}
var
begin
end;
{$ELSE}
begin
end;
{$ENDIF}
procedure TAutoUpdate.SetCheckURL(const Value: String);
begin
end;
end.
使用了网友的取版本号及调用apk,这里谢过!
调用方法:
procedure TMainForm.Button1Click(Sender: TObject);
begin
end;
转自:http://blog.sina.com.cn/s/blog_44fa172f0102vgd1.html