Author: Nikolas M41894
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<span class="hljs-keyword" style="color: #000080; font-weight: bold;">unit</span> uOSVersionAndLocale; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">interface</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">uses</span> System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Platform <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF Android}</span> ,Androidapi.JNI.Os <span class="hljs-comment" style="color: #008000; font-weight: bold;">//TJBuild</span> ,Androidapi.Helpers <span class="hljs-comment" style="color: #008000; font-weight: bold;">// StringToJString</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF IOS}</span> ,iOSapi.UIKit ,Posix.SysSysctl ,Posix.StdDef <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> ; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">type</span> <span class="hljs-title" style="color: #0000ff; font-weight: bold;">TForm1</span> = <span class="hljs-keyword" style="color: #000080; font-weight: bold;">class</span>(TForm) Memo1: TMemo; ToolBar1: TToolBar; Button1: TButton; <span class="hljs-function"><span class="hljs-keyword" style="color: #000080; font-weight: bold;">procedure</span> <span class="hljs-title" style="color: #0000ff; font-weight: bold;">Button1Click</span><span class="hljs-params">(Sender: TObject)</span>;</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">private</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF IOS}</span> <span class="hljs-function"><span class="hljs-keyword" style="color: #000080; font-weight: bold;">function</span> <span class="hljs-title" style="color: #0000ff; font-weight: bold;">GetDeviceModelString</span>:</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">String</span>; <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{ private 宣言 }</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">public</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{ public 宣言 }</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">var</span> Form1: TForm1; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">implementation</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$R *.fmx}</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF IOS}</span> <span class="hljs-function"><span class="hljs-keyword" style="color: #000080; font-weight: bold;">function</span> <span class="hljs-title" style="color: #0000ff; font-weight: bold;">TForm1</span>.<span class="hljs-title" style="color: #0000ff; font-weight: bold;">GetDeviceModelString</span>:</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">String</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">var</span> Size: size_t; DeviceModelBuffer: <span class="hljs-keyword" style="color: #000080; font-weight: bold;">array</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">of</span> Byte; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">begin</span> sysctlbyname(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'hw.machine'</span>, <span class="hljs-keyword" style="color: #000080; font-weight: bold;">nil</span>, @Size, <span class="hljs-keyword" style="color: #000080; font-weight: bold;">nil</span>, <span class="hljs-number">0</span>); <span class="hljs-keyword" style="color: #000080; font-weight: bold;">if</span> Size > <span class="hljs-number">0</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">then</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">begin</span> SetLength(DeviceModelBuffer, Size); sysctlbyname(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'hw.machine'</span>, @DeviceModelBuffer[<span class="hljs-number">0</span>], @Size, <span class="hljs-keyword" style="color: #000080; font-weight: bold;">nil</span>, <span class="hljs-number">0</span>); Result := UTF8ToString(MarshaledAString(DeviceModelBuffer)); <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">else</span> Result := EmptyStr; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span>; <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> <span class="hljs-function"><span class="hljs-keyword" style="color: #000080; font-weight: bold;">procedure</span> <span class="hljs-title" style="color: #0000ff; font-weight: bold;">TForm1</span>.<span class="hljs-title" style="color: #0000ff; font-weight: bold;">Button1Click</span><span class="hljs-params">(Sender: TObject)</span>;</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">var</span> OSVersion: TOSVersion; OSLang: <span class="hljs-keyword" style="color: #000080; font-weight: bold;">String</span>; LocaleService: IFMXLocaleService; ModelName: <span class="hljs-keyword" style="color: #000080; font-weight: bold;">String</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">begin</span> ModelName := <span class="hljs-string" style="color: #0000ff; font-weight: bold;">'unknown'</span>; <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF Android}</span> ModelName := JStringToString(TJBuild.JavaClass.MODEL); <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$IFDEF IOS}</span> ModelName := GetDeviceModelString; <span class="hljs-comment" style="color: #008000; font-weight: bold;">{$ENDIF}</span> Memo1.Lines.Add(Format(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'ModelName=%s'</span>, [ ModelName ] )); Memo1.Lines.Add(Format(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'OSName=%s'</span>, [OSVersion.Name])); Memo1.Lines.Add(Format(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'Platform=%d'</span>, [Ord(OSVersion.Platform)])); Memo1.Lines.Add(Format(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'Version=%d.%d'</span>, [OSVersion.Major,OSVersion.Minor])); OSLang := <span class="hljs-string" style="color: #0000ff; font-weight: bold;">''</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">if</span> TPlatformServices.Current.SupportsPlatformService(IFMXLocaleService, IInterface(LocaleService)) <span class="hljs-keyword" style="color: #000080; font-weight: bold;">then</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">begin</span> OSLang := LocaleService.GetCurrentLangID(); <span class="hljs-comment" style="color: #008000; font-weight: bold;">// if set Japanese on Android, LocaleService returns "jp", but other platform returns "ja"</span> <span class="hljs-comment" style="color: #008000; font-weight: bold;">// so I think it is better to change "jp" to "ja"</span> <span class="hljs-keyword" style="color: #000080; font-weight: bold;">if</span> (OSLang = <span class="hljs-string" style="color: #0000ff; font-weight: bold;">'jp'</span>) <span class="hljs-keyword" style="color: #000080; font-weight: bold;">then</span> OSLang := <span class="hljs-string" style="color: #0000ff; font-weight: bold;">'ja'</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span>; Memo1.Lines.Add(Format(<span class="hljs-string" style="color: #0000ff; font-weight: bold;">'Lang=%s'</span>, [ OSLang ] )); <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span>; <span class="hljs-keyword" style="color: #000080; font-weight: bold;">end</span>. |
Do you want to build an Android app? Try the Android App Builder Software, which will assist you in developing apps in the Delphi or C++ environments.
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition