学习使用Delphi for android 调用Java类库

http://blog.csdn.net/laorenshen/article/details/41148253

学习使用Delphi for android 调用Java类库

2014-11-15 18:36 1053人阅读 评论(0) 收藏 举报
学习使用Delphi for android 调用Java类库 分类:
安卓开发(27) 学习使用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;

end;

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

注意命名规范前面加J, 大小写保持后Java一致;

注意自定义标签写法

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

3. 工厂类及delphi封装的调用类

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

注意以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!');

tts.speak(text, TJTextToSpeech.JavaClass.QUEUE_FLUSH, nil);

end;

上一篇:jquery特效收藏第一期


下一篇:phpcms 细节