Sign in Join
Wiki copytemporary

Android Programming/ja: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11See also [[Custom Drawn Interface/Android/ja]]
22
33※今のところ、WindowsでAndroidプログラミングを実際に出来るようにすることを優先目標として記述しています。そのため、原文の翻訳となっていないところが多いです。間違い等はどんどん修正していただけると幸いです。(malcome)\
44
55Knowing general Android programming can be very useful to help develop the Lazarus Android Interface.
66
77## How to...
88
99### Build the NDK OpenGL example
1010
1111Just follow these steps:
1212
1313**Step 1** - Download and install the Android NDK, Android SDK and Ant. More information here: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones]]
1414
1515**Step 2** - Install the pre-compiled FPC cross-compiler. Instructions here: [[Android Interface|Android_Interface#Using_the_pre-compiled_compiler]]
1616
1717**Step 3** - Download the latest lazarus-ccr sourceforge code:
1818
1919``` bash
2020svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr lazarus-ccr
2121```
2222
2323or if you think it is too big you can download just the folder lazarus-ccr/bindings/android-ndk\
2424
2525※ andoroid-ndk バインディングは Lazarus をインスト-ルしたフォルダの、lcl\interfaces\customdrawn\android にもあるのでそれを使えば必要十分であると思います。\
2626
2727**Step 4** - Building the Pascal Library
2828
2929> **Note:** In the steps below, the directory /home/felipe/Programas or ~/Programas is used. Please adjust if your paths are different.
3030
3131Open the project lazarus-ccr/bindings/android-ndk/examples/opengltest/opengltest.lpi
3232
3333Go to Project-\>Project Options-\>Paths and where is written "Libraries -Fl" you should see a value like this:
3434
35-```
35+```text
3636/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib
3737```
3838
3939Change it to the correct path in your system which points to the NDK and to the library folder for your minimal required Android API Level
4040
4141Now build the project using Lazarus.
4242
4343**Step 5** - Configuring the build environment
4444
4545Open the file opengltest/local.properties. Change this line to point to your SDK location:
4646
47-```
47+```text
4848sdk.dir=/home/felipe/Programas/android-sdk-linux
4949```
5050
5151**Step 6** - Buildings the APK
5252
5353Open a terminal and type these commands:
5454
5555``` bash
5656cd lazarus-ccr/bindings/ndk/examples/opengltest/android
5757ant debug
5858```
5959
6060The APK file will be placed in opengltest/android/bin/
6161
6262**Step 7** - Install the APK
6363
6464In this step, if you get error messages about permissions read: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Recognition_of_devices_under_Linux]]
6565
6666The command to install our APK in the phone is:
6767
6868``` bash
6969cd opengltest/android
7070~/Programas/android-sdk-linux/platform-tools/adb install bin/OpenGLNDKTest-debug.apk
7171```
7272
7373You should see something like:
7474
75-```
75+```text
76762395 KB/s (107299 bytes in 0.043s)
7777 pkg: /data/local/tmp/OpenGLNDKTest-debug.apk
7878Success
7979```
8080
8181If you get an error message that it is already installed you can install with this command:
8282
8383``` bash
8484~/Programas/android-sdk-linux/platform-tools/adb uninstall com.pascal.opengltest
8585```
8686
8787And then you can run adb logcat to see what the log says while you start it in the phone via its newly added icon:
8888
8989``` bash
9090~/Programas/android-sdk-linux/platform-tools/adb logcat
9191```
9292
9393### Read the screen metrics
9494
9595First fill a TDisplayMetrics with the correct info:
9696
9797``` pascal
9898uses androidutil;
9999
100100var
101101 MyDisplayMetrics: TDisplayMetrics;
102102 Str: string;
103103 //
104104 lHeight, lWidth: Integer;
105105 xdpi, ydpi, lScreenSize: Single;
106106begin
107107 // ..
108108 // Objects
109109
110110 MyDisplayMetrics := TDisplayMetrics.Create;
111111 Activity.getWindowManager().getDefaultDisplay().getMetrics(MyDisplayMetrics);
112112```
113113
114114And then you can read it from the TDisplayMetrics:
115115
116116``` pascal
117117 lHeight := MyDisplayMetrics.heightPixels();
118118 lWidth := MyDisplayMetrics.widthPixels();
119119 xdpi := MyDisplayMetrics.xdpi();
120120 ydpi := MyDisplayMetrics.ydpi();
121121 lScreenSize := sqrt(sqr(lHeight / ydpi) + sqr(lWidth / xdpi));
122122 ldensity := MyDisplayMetrics.density();
123123 ldensityDpi := MyDisplayMetrics.densityDpi();
124124 scaledDensity := MyDisplayMetrics.scaledDensity();
125125```
126126
127127Note that lots of devices lie about the xdpi and ydpi, so don't trust the lScreenSize calculated above. Smartphones might report even 10 inches of screen size, while the correct is around 4.
128128
129129## Details about Android devices
130130
131131Since there are so many Android devices, it can be useful to keep track of some information about them. See also in the wikipedia Comparison of Android Devices [1](http://en.wikipedia.org/wiki/Comparison_of_Android_devices) and details about the processors in [2](https://wiki.mozilla.org/Mobile/Platforms/Android#System_Requirements).
132132
133133### Smartphones
134134
135135| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
136136|----|----|----|----|----|----|----|
137137| HTC | Wildfire | HTC Wildfire | armv6 | 2.1=\>2.2 | Yes\|\|- | |
138138| Sony Erricson | Xperia X10 | X10i | armv7 | 1.6=\>2.1 | No\|\|- | |
139139
140140### Tablets
141141
142142| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
143143|----|----|----|----|----|----|----|
144144| Toshiba | Folio 100 Tablet | TOSHIBA_FOLIO_AND_A | armv7 | 2.2 | Yes\|\|- | |
145145
146146### List of devices ordered by processor compatibility (ARMv6 vs ARMv7)
147147
148148**List of ARMv7 devices:**
149149
150150- Advent Vega (P10AN01)
151151- Dell Streak, Streak 7
152152- HTC Desire
153153- HTC Desire Z (T-Mobile G2)
154154- HTC Desire HD
155155- HTC Droid Incredible
156156- HTC EVO 4G, EVO Shift 4G
157157- HTC Glacier (T-Mobile myTouch 4G)
158158- HTC Inspire 4G
159159- HTC Nexus One
160160- HTC Thunderbolt 4G
161161- Huawei Ideos S7
162162- LG Optimus Z
163163- Motorola Atrix 4G
164164- Motorola Bravo
165165- Motorola Cliq 2 - untested
166166- Motorola Defy
167167- Motorola Droid 2, Droid 2 Global
168168- Motorola Droid Pro (Motorola PRO)
169169- Motorola Droid X
170170- Motorola Xoom
171171- POV Mobii Tegra Tablet
172172- Samsung Continuum (i400)
173173- Samsung Galaxy S (i9000, Captivate, Fascinate, Vibrant, Epic 4G)
174174- Samsung Galaxy Tab
175175- Sharp IS03
176176- Sony Ericsson Xperia X10
177177- Toshiba AS100
178178- Viewsonic gTablet
179179- Acer Liquid E
180180- Acer Liquid (Liquid A1)
181181- Archos 101 Internet Tablet
182182- Motorola Charm
183183- Motorola Droid (Milestone)
184184- Samsung Galaxy S 4G
185185- Samsung Nexus S
186186
187187**List of ARMv6 processors:**
188188
189189- Android SDK emulator
190190- Asus Garmin nuvifone A50 (T-Mobile Garminfone)
191191- Augen GENTouch 78 Tablet
192192- Coby Kyros Internet Tablet (MID7015)
193193- Geeksphone One, Geeksphone Zero
194194- HTC Aria
195195- HTC ChaCha
196196- HTC Dream (T-Mobile G1, Android Dev Phone 1)
197197- HTC Droid Eris
198198- HTC Espresso (T-Mobile myTouch 3G Slide)
199199- HTC Hero (T-Mobile G2 Touch)
200200- HTC Legend
201201- HTC Magic (T-Mobile myTouch 3G, T-Mobile G1 Touch)
202202- HTC Salsa
203203- HTC Tattoo
204204- HTC Wildfire
205205- Huawei Ascend
206206- Huawei Ideos U8150-B (T-Mobile Comet)
207207- Huawei U8110 (T-Mobile Pulse Mini)
208208- Huawei U8230
209209- LG Ally (Apex) (LG VS740)
210210- LG GW620 (Eve, InTouch Max, LinkMe)
211211- LG Optimus, Optimus M, Optimus T, Optimus S, Optimus V
212212- LG Vortex
213213- LG P500
214214- MAG iMiTO iM7
215215- vMAG iMiTO iM7S
216216- Motorola Backflip
217217- Motorola Citrus
218218- Motorola Cliq (MB200)
219219- Motorola Dext
220220- Motorola Devour
221221- Motorola i1
222222- Motorola Spice XT300
223223- Motorola Quench XT5 XT502
224224- Pandigital Novel
225225- Samsung GT-S5570 Galaxy Mini
226226- Samsung i5500 Galaxy 5 (Corby)
227227- Samsung i5700 Galaxy Portal (Spica)
228228- Samsung i5800 Galaxy 3
229229- Samsung i7500 Galaxy
230230- Samsung Intercept
231231- Samsung M900 Moment
232232- Samsung S5830 Galaxy Ace
233233- Samsung Transform
234234- Sanyo ZIO M6000
235235- Sony Ericsson Xperia X8
236236- Sony Ericsson Xperia X10 Mini
237237- Sony Ericsson Xperia X10 Mini Pro
238238- Superpad 10.2" Tablet PC
239239- Viewsonic ViewPad 7 Tablet
240240- Velocity Micro T103 Cruz tablet
241241- Vodafone 845
242242- ZTE Blade
243243
244244### Playing sounds and videos on the phones
245245
246246Each phone comes with different codecs installed, which means that the native Media Player will be able to play different formats in different phones. Below is a table showing which formats play in the native Media Player of each phone:
247247
248248| Telephone | Android | webm - VP8 | m4v - H.264 | ogv - Theora | mp4 - H.264 | mov - H.264 | avi - RLE | mpg - MPEG-1 | wmv - WM9 | 3gp - MPEG-4 |
249249|----|----|----|----|----|----|----|----|----|----|----|
250250| Emulator | 1.6 | x | x | x | x | x | x | x | x | OK |
251251| Emulator | 2.1 | x | x | x | x | x | x | x | x | OK |
252252| Nexus One | 2.2 | x | OK | x | OK | x | x | x | x | OK |
253253| HTC Desire | 1.6 | x | OK | x | OK | x | x | x | OK | OK |
254254| Toshiba Folio 100 | 2.2 | x | OK | x | OK | x | x | x | OK | OK |
255255| Xperia X10 | 2.1 | x | OK | x | x | x | x | x | OK | OK |
256256| Xperia X8 | 2.2 | x | OK | x | x | x | x | x | x | OK |
257257| HTC Wildfire | 2.1 | x | OK | x | x | x | x | x | x | OK |
258258| HTC Desire HD | 2.2 | x | OK | x | OK | x | x | x | x | OK |
259259| Galaxy Tab | 2.2 | x | OK | x | x | x | x | x | x | OK |
260260| myPhone A210 | 1.6 | x | OK | x | x | x | x | x | x | OK |
261261| Motorola Milestone | 2.1 | x | OK | x | x | x | x | x | OK | ? |
262262| Milestone 2 | 2.2 | x | OK | x | x | x | x | x | sound-only | OK |
263263
264264## Android file system
265265
266266Android recommends to use getApplicationContext().getFilesDir() to obtain the data directory for the application and this routine in my HTC Wildfire returns /data/data/\<package name\>
267267
268268The native libraries of the application are placed in /data/data/\<package\>/lib
269269
270270A good place where we have read and write access and we can create new files and even run files is /data/data/\<package name\>
271271
272272## Creating a new Java Android application
273273
274274This info can be useful for helping implement LCL-CustomDrawn-Android
275275
276276Generic instructions here: <http://developer.android.com/guide/developing/other-ide.html>
277277
278278### Showing/hiding the virtual keyboard
279279
280280See here: <http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html>
281281
282282To show it:
283283
284284``` pascal
285285InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
286286imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
287287```
288288
289289and for hiding the keyboard:
290290
291291``` pascal
292292InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
293293imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);
294294```
295295
296296## Android JNI
297297
298298Using JNI (Java Native Interface) you can call most Android SDK APIs and also other Java libraries.
299299
300300### Table of Java types and Pascal JNI types
301301
302302All of the Java types except interfaces can be represented in JNI without problems. For each Java type there is a corresponding VM Type Signature which is utilized in routines such as javaEnvRef^^.GetMethodID. There is also the JNI native type, which is the type declared in the unit lazarus/lcl/interfaces/customdrawn/android/jni.pas and should be the type utilized in JNI code written in Pascal. The Pascal equivalent is provided just for comparison, as one should use the types from jni.pas when writing JNI code.
303303
304304| Java type | VM signature | Type in jni.pas | Pascal equivalent | Description |
305305|----|----|----|----|----|
306306| boolean | Z | jboolean | Boolean | |
307307| byte | B | jbyte | Byte | |
308308| char | C | jchar | Word | |
309309| short | S | jshort | Shortint | |
310310| int | I | jint | Integer | |
311311| long | J | jlong | Int64 | |
312312| float | F | jfloat | Single | |
313313| double | D | jdouble | Double | |
314314| a class, ie: String or InputStream | L\<full class name\> ie: Ljava/lang/String; | jobject | N/A | |
315315| Array, ie: int\[\] | \[\<type-name\> ie: \[I | N/A | | |
316316| A method type | (arg-types)ref-type | JMethodID | N/A | |
317317| void | V | N/A | procedure | |
318318
319319### Example 1: Creating a Java object and calling a Java method
320320
321321Let's say that you want to make Pascal JNI code equivalent to the following Java code:
322322
323323``` java
324324 HttpGet javaRequest = new HttpGet();
325325 URI myURI = new URI("http://magnifier.sourceforge.net/");
326326 javaRequest.setURI(myURI);
327327```
328328
329329First you need to get all Class IDs involved in the process, then get all method IDs which we will use and then make the calls. If the call has parameters, then pass it in an array. The following Pascal native code is equivalent to that Java code above:
330330
331331``` pascal
332332uses jni, customdrawnint;
333333
334334var
335335 javaClass_HttpGet, javaClass_URI: jclass;
336336 javaMethod_HttpGet_new, javaMethod_HttpGet_setURI,
337337 javaMethod_URI_new: jmethodid;
338338 javaRequest, javaURI: jobject;
339339 javaString: jstring;
340340 lParams: array[0..2] of JValue;
341341 lNativeString: PChar;
342342begin
343343 DebugLn(':>LoadHTMLPageViaJNI');
344344 // First call FindClass for all required classes
345345 javaClass_HttpGet := javaEnvRef^^.FindClass(javaEnvRef,
346346 'org/apache/http/client/methods/HttpGet');
347347 javaClass_URI := javaEnvRef^^.FindClass(javaEnvRef,
348348 'java/net/URI');
349349
350350 // Now all Method IDs
351351 DebugLn(':LoadHTMLPageViaJNI 1');
352352 javaMethod_HttpGet_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, '<init>', '()V');
353353 javaMethod_HttpGet_setURI := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, 'setURI', '(Ljava/net/URI;)V');
354354 javaMethod_URI_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_URI, '<init>', '(Ljava/lang/String;)V');
355355
356356 // Create a new instance for the HTTP request object
357357 // HttpGet javaRequest = new HttpGet();
358358 javaRequest := javaEnvRef^^.NewObject(javaEnvRef,
359359 javaClass_HttpGet,
360360 javaMethod_HttpGet_new);
361361
362362 DebugLn(':LoadHTMLPageViaJNI 4 javaRequest='+IntToHex(PtrInt(javaRequest), 8));
363363 // Add a URI for the request object
364364 // URI javaURI = new URI("http://magnifier.sourceforge.net/");
365365 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, 'http://magnifier.sourceforge.net');
366366 javaURI := javaEnvRef^^.NewObjectA(javaEnvRef,
367367 javaClass_URI, javaMethod_URI_new, @lParams[0]);
368368 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
369369 // javaRequest.setURI(javaURI);
370370 lParams[0].l := javaURI;
371371 javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaRequest,
372372 javaMethod_HttpGet_setURI, @lParams[0]);
373373```
374374
375375### Example 2: Calling a static method
376376
377377Let's say that you want to call the following static method located in the Bass class: <http://jerome.jouvie.free.fr/nativebass/javadoc/org/jouvieje/bass/Bass.html>
378378
379379``` java
380380 public static native int BASS_StreamCreateFile(String file, long offset, long length, int flags);
381381```
382382
383383First you need to get all Class IDs involved in the process, then get the static method ID and then make the call with the parameter in the array:
384384
385385``` pascal
386386uses jni, customdrawnint;
387387
388388var
389389 javaClass_Bass: jclass;
390390 javaMethod_Bass_BASS_StreamCreateFile: jmethodid;
391391 javaString: jstring;
392392 lParams: array[0..3] of JValue;
393393 lRetInt: jint;
394394begin
395395 // First call FindClass for all required classes
396396 javaClass_Bass := javaEnvRef^^.FindClass(javaEnvRef, 'org/jouvieje/bass/Bass');
397397
398398 // Now all Method IDs
399399 javaMethod_Bass_BASS_StreamCreateFile := javaEnvRef^^.GetStaticMethodID(javaEnvRef, javaClass_Bass, 'BASS_StreamCreateFile', '(Ljava/lang/String;JJI)I');
400400
401401 // Make the call
402402 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, '/path/to/file');
403403 lParams[1].j := 0; // offset
404404 lParams[2].j := 200; // length
405405 lParams[3].i := 0; // flags
406406 lRetInt := javaEnvRef^^.CallStaticIntMethodA(javaEnvRef, javaClass_Bass, javaMethod_Bass_BASS_StreamCreateFile, @lParams[0]);
407407 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
408408```
409409
410410## References
411411
412412- <http://developer.android.com/guide/developing/other-ide.html>
413413- [Tutorial "Lazarus and Android"](https://dl.dropboxusercontent.com/u/3753548/Lazarus%20and%20Android.pdf) (PDF file)
414414
415415---
416416*From the Free Pascal wiki, page "Android Programming/ja", as it stood on 2020-02-09 after a change by GAN, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
417417Category: Android/ja

Back to history