天天看點

學習使用Delphi for android 調用Java類庫

Android是一套非常優秀的移動端作業系統,Delphi Xe5中也能直接調用SDK中提供的各項API,但是delphi自己封裝的AndroidAPI非常少,在安裝目錄的 source\rtl\android 中,而且所提供的android API也沒有明确sdk版本差別,很多事情都得我們自己來封裝了完成。

在Delphi中将java類的通過接口進行包裝,分為類方法接口及類執行個體方法接口兩個部分,然後通過一種工廠方法将這兩種接口混合起來,對外提供統一的java執行個體服務;

類方法:顧名思義就是屬于類本身,不依賴類執行個體化就可調用的方法,類方法包裝,主要是指包裝java中靜态屬性,靜态方法,構造方法(構造函數包裝需要将構造函數命名為init,預設是一個,也可以多個或者沒有);

類執行個體:就是依賴執行個體化類後才能調用的方法;

有了上面的概念,下面針對java的接口包裝及類包裝分别進行說明,所引用的代碼為Jeff編寫的android API 19 的語音朗讀引擎接口包android.speech.tts.TextToSpeech的Delphi實作Androidapi.JNI.TTS.pas;

l 接口(java)

1. 類方法包裝(繼承IJavaClass)

JTextToSpeech_OnInitListenerClass = interface(IJavaClass)

['{58D32EFB-6528-4EC6-BA4F-28B22FE8E573}']

end;

注意命名規範,這樣利于你,也利于大家J打頭Class結尾,大小寫保持後Java一緻

2. 執行個體方法包裝(繼承IJavaInstance)

[JavaSignature('android/speech/tts/SynthesisCallback')]

JSynthesisCallback = interface(IJavaInstance)

['{190C13DE-BC7E-44CE-94F6-4AF1A84A4612}']

function audioAvailable(buffer: TJavaArray<byte>; offset: Integer; length: Integer): Integer; cdecl;

function done: Integer; cdecl;

procedure error; cdecl;

function getMaxBufferSize: Integer; cdecl;

function start(sampleRateInHz: Integer; audioFormat: Integer; channelCount: Integer): Integer; cdecl;

TJSynthesisCallback = class(TJavaGenericImport<JSynthesisCallbackClass, JSynthesisCallback>) end;

注意命名規範前面加J, 大小寫保持後Java一緻;

注意自定義标簽寫法

3. 工廠類及delphi封裝的調用類

注意以T打頭;

l 類(java)

1. 類方法包裝(繼承JObjectClass)

2. 執行個體方法包裝(繼承JObject)

3. 工廠類及delphi封裝的調用類與接口相同;

Java類中各類型Delphi在Androidapi.JNI.JavaTypes也做了相應的封裝,也有相應對照表在源碼注釋中,可對照參考

JObject = interface;//java.lang.Object

JClassLoader = interface;//java.lang.ClassLoader

JInputStream = interface;//java.io.InputStream

JByteArrayInputStream = interface;//java.io.ByteArrayInputStream

JNumber = interface;//java.lang.Number

JInteger = interface;//java.lang.Integer

JBoolean = interface;//java.lang.Boolean

JByte = interface;//java.lang.Byte

JEnumeration = interface;//java.util.Enumeration

JCalendar = interface;//java.util.Calendar

JAbstractCollection = interface;//java.util.AbstractCollection

JAbstractSet = interface;//java.util.AbstractSet

JHashSet = interface;//java.util.HashSet

JAbstractList = interface;//java.util.AbstractList

JSerializable = interface;//java.io.Serializable

JEnum = interface;//java.lang.Enum

JThread_State = interface;//java.lang.Thread$State

JThread_UncaughtExceptionHandler = interface;//java.lang.Thread$UncaughtExceptionHandler

JFileDescriptor = interface;//java.io.FileDescriptor

JFile = interface;//java.io.File

JArrayList = interface;//java.util.ArrayList

JRunnable = interface;//java.lang.Runnable

JCloseable = interface;//java.io.Closeable

JWriter = interface;//java.io.Writer

JIterable = interface;//java.lang.Iterable

JCollection = interface;//java.util.Collection

JList = interface;//java.util.List

JOutputStream = interface;//java.io.OutputStream

Jlang_Class = interface;//java.lang.Class

JThrowable = interface;//java.lang.Throwable

JException = interface;//java.lang.Exception

JJSONException = interface;//org.json.JSONException

JUUID = interface;//java.util.UUID

JAbstractMap = interface;//java.util.AbstractMap

JHashMap = interface;//java.util.HashMap

JRandom = interface;//java.util.Random

JAnnotation = interface;//java.lang.annotation.Annotation

JThread = interface;//java.lang.Thread

JDate = interface;//java.util.Date

JFilterOutputStream = interface;//java.io.FilterOutputStream

JPrintStream = interface;//java.io.PrintStream

JRuntimeException = interface;//java.lang.RuntimeException

JObserver = interface;//java.util.Observer

JFloat = interface;//java.lang.Float

JDouble = interface;//java.lang.Double

JPrintWriter = interface;//java.io.PrintWriter

JIterator = interface;//java.util.Iterator

JListIterator = interface;//java.util.ListIterator

JByteArrayOutputStream = interface;//java.io.ByteArrayOutputStream

JStackTraceElement = interface;//java.lang.StackTraceElement

JFileOutputStream = interface;//java.io.FileOutputStream

JAbstractStringBuilder = interface;//java.lang.AbstractStringBuilder

JStringBuilder = interface;//java.lang.StringBuilder

JCharSequence = interface;//java.lang.CharSequence

JGregorianCalendar = interface;//java.util.GregorianCalendar

JJSONTokener = interface;//org.json.JSONTokener

JMap = interface;//java.util.Map

JLocale = interface;//java.util.Locale

JTimeZone = interface;//java.util.TimeZone

JFileFilter = interface;//java.io.FileFilter

JEnumSet = interface;//java.util.EnumSet

Jutil_Observable = interface;//java.util.Observable

JFilenameFilter = interface;//java.io.FilenameFilter

JJSONObject = interface;//org.json.JSONObject

JString = interface;//java.lang.String

JSet = interface;//java.util.Set

JShort = interface;//java.lang.Short

JThreadGroup = interface;//java.lang.ThreadGroup

JComparator = interface;//java.util.Comparator

JJSONArray = interface;//org.json.JSONArray

JLong = interface;//java.lang.Long

JFileInputStream = interface;//java.io.FileInputStream

JStringBuffer = interface;//java.lang.StringBuffer

此單元也包含了部分轉換函數如StringToJString 各位可以跟進源碼察看;

l 如何調用

直接看代碼把

var

text : JString;

begin

tts := TJTextToSpeech.JavaClass.init(SharedActivityContext, ttsListener);

text := StringToJString('hello!');