注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

鑫淼梦园的博客

圆你的梦想 从这里开始

 
 
 

日志

 
 

Delphi XE8 利用HttpClient实现的一个App自动更新组件  

2015-06-03 13:03:59|  分类: delphi xe8 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
第一个版本,分享了。
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;

  评论这张
 
阅读(327)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017