Sign in Join
Wiki copytemporary

Android Programming/ja: what changed

Revision 2 to 3. 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-`/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib`
35+```
36+/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib
37+```
3638
3739Change 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
3840
3941Now build the project using Lazarus.
4042
4143**Step 5** - Configuring the build environment
4244
4345Open the file opengltest/local.properties. Change this line to point to your SDK location:
4446
45-`sdk.dir=/home/felipe/Programas/android-sdk-linux`
47+```
48+sdk.dir=/home/felipe/Programas/android-sdk-linux
49+```
4650
4751**Step 6** - Buildings the APK
4852
4953Open a terminal and type these commands:
5054
5155``` bash
5256cd lazarus-ccr/bindings/ndk/examples/opengltest/android
5357ant debug
5458```
5559
5660The APK file will be placed in opengltest/android/bin/
5761
5862**Step 7** - Install the APK
5963
6064In 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]]
6165
6266The command to install our APK in the phone is:
6367
6468``` bash
6569cd opengltest/android
6670~/Programas/android-sdk-linux/platform-tools/adb install bin/OpenGLNDKTest-debug.apk
6771```
6872
6973You should see something like:
7074
71-`2395 KB/s (107299 bytes in 0.043s)`\
72-`       pkg: /data/local/tmp/OpenGLNDKTest-debug.apk`\
73-`Success`
75+```
76+2395 KB/s (107299 bytes in 0.043s)
77+ pkg: /data/local/tmp/OpenGLNDKTest-debug.apk
78+Success
79+```
7480
7581If you get an error message that it is already installed you can install with this command:
7682
7783``` bash
7884~/Programas/android-sdk-linux/platform-tools/adb uninstall com.pascal.opengltest
7985```
8086
8187And then you can run adb logcat to see what the log says while you start it in the phone via its newly added icon:
8288
8389``` bash
8490~/Programas/android-sdk-linux/platform-tools/adb logcat
8591```
8692
8793### Read the screen metrics
8894
8995First fill a TDisplayMetrics with the correct info:
9096
9197``` pascal
9298uses androidutil;
9399
94100var
95101 MyDisplayMetrics: TDisplayMetrics;
96102 Str: string;
97103 //
98104 lHeight, lWidth: Integer;
99105 xdpi, ydpi, lScreenSize: Single;
100106begin
101107 // ..
102108 // Objects
103109
104110 MyDisplayMetrics := TDisplayMetrics.Create;
105111 Activity.getWindowManager().getDefaultDisplay().getMetrics(MyDisplayMetrics);
106112```
107113
108114And then you can read it from the TDisplayMetrics:
109115
110116``` pascal
111117 lHeight := MyDisplayMetrics.heightPixels();
112118 lWidth := MyDisplayMetrics.widthPixels();
113119 xdpi := MyDisplayMetrics.xdpi();
114120 ydpi := MyDisplayMetrics.ydpi();
115121 lScreenSize := sqrt(sqr(lHeight / ydpi) + sqr(lWidth / xdpi));
116122 ldensity := MyDisplayMetrics.density();
117123 ldensityDpi := MyDisplayMetrics.densityDpi();
118124 scaledDensity := MyDisplayMetrics.scaledDensity();
119125```
120126
121127Note 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.
122128
123129## Details about Android devices
124130
125131Since 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).
126132
127133### Smartphones
128134
129135| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
130136|----|----|----|----|----|----|----|
131137| HTC | Wildfire | HTC Wildfire | armv6 | 2.1=\>2.2 | Yes\|\|- | |
132138| Sony Erricson | Xperia X10 | X10i | armv7 | 1.6=\>2.1 | No\|\|- | |
133139
134140### Tablets
135141
136142| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
137143|----|----|----|----|----|----|----|
138144| Toshiba | Folio 100 Tablet | TOSHIBA_FOLIO_AND_A | armv7 | 2.2 | Yes\|\|- | |
139145
140146### List of devices ordered by processor compatibility (ARMv6 vs ARMv7)
141147
142148**List of ARMv7 devices:**
143149
144150- Advent Vega (P10AN01)
145151- Dell Streak, Streak 7
146152- HTC Desire
147153- HTC Desire Z (T-Mobile G2)
148154- HTC Desire HD
149155- HTC Droid Incredible
150156- HTC EVO 4G, EVO Shift 4G
151157- HTC Glacier (T-Mobile myTouch 4G)
152158- HTC Inspire 4G
153159- HTC Nexus One
154160- HTC Thunderbolt 4G
155161- Huawei Ideos S7
156162- LG Optimus Z
157163- Motorola Atrix 4G
158164- Motorola Bravo
159165- Motorola Cliq 2 - untested
160166- Motorola Defy
161167- Motorola Droid 2, Droid 2 Global
162168- Motorola Droid Pro (Motorola PRO)
163169- Motorola Droid X
164170- Motorola Xoom
165171- POV Mobii Tegra Tablet
166172- Samsung Continuum (i400)
167173- Samsung Galaxy S (i9000, Captivate, Fascinate, Vibrant, Epic 4G)
168174- Samsung Galaxy Tab
169175- Sharp IS03
170176- Sony Ericsson Xperia X10
171177- Toshiba AS100
172178- Viewsonic gTablet
173179- Acer Liquid E
174180- Acer Liquid (Liquid A1)
175181- Archos 101 Internet Tablet
176182- Motorola Charm
177183- Motorola Droid (Milestone)
178184- Samsung Galaxy S 4G
179185- Samsung Nexus S
180186
181187**List of ARMv6 processors:**
182188
183189- Android SDK emulator
184190- Asus Garmin nuvifone A50 (T-Mobile Garminfone)
185191- Augen GENTouch 78 Tablet
186192- Coby Kyros Internet Tablet (MID7015)
187193- Geeksphone One, Geeksphone Zero
188194- HTC Aria
189195- HTC ChaCha
190196- HTC Dream (T-Mobile G1, Android Dev Phone 1)
191197- HTC Droid Eris
192198- HTC Espresso (T-Mobile myTouch 3G Slide)
193199- HTC Hero (T-Mobile G2 Touch)
194200- HTC Legend
195201- HTC Magic (T-Mobile myTouch 3G, T-Mobile G1 Touch)
196202- HTC Salsa
197203- HTC Tattoo
198204- HTC Wildfire
199205- Huawei Ascend
200206- Huawei Ideos U8150-B (T-Mobile Comet)
201207- Huawei U8110 (T-Mobile Pulse Mini)
202208- Huawei U8230
203209- LG Ally (Apex) (LG VS740)
204210- LG GW620 (Eve, InTouch Max, LinkMe)
205211- LG Optimus, Optimus M, Optimus T, Optimus S, Optimus V
206212- LG Vortex
207213- LG P500
208214- MAG iMiTO iM7
209215- vMAG iMiTO iM7S
210216- Motorola Backflip
211217- Motorola Citrus
212218- Motorola Cliq (MB200)
213219- Motorola Dext
214220- Motorola Devour
215221- Motorola i1
216222- Motorola Spice XT300
217223- Motorola Quench XT5 XT502
218224- Pandigital Novel
219225- Samsung GT-S5570 Galaxy Mini
220226- Samsung i5500 Galaxy 5 (Corby)
221227- Samsung i5700 Galaxy Portal (Spica)
222228- Samsung i5800 Galaxy 3
223229- Samsung i7500 Galaxy
224230- Samsung Intercept
225231- Samsung M900 Moment
226232- Samsung S5830 Galaxy Ace
227233- Samsung Transform
228234- Sanyo ZIO M6000
229235- Sony Ericsson Xperia X8
230236- Sony Ericsson Xperia X10 Mini
231237- Sony Ericsson Xperia X10 Mini Pro
232238- Superpad 10.2" Tablet PC
233239- Viewsonic ViewPad 7 Tablet
234240- Velocity Micro T103 Cruz tablet
235241- Vodafone 845
236242- ZTE Blade
237243
238244### Playing sounds and videos on the phones
239245
240246Each 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:
241247
242248| 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 |
243249|----|----|----|----|----|----|----|----|----|----|----|
244250| Emulator | 1.6 | x | x | x | x | x | x | x | x | OK |
245251| Emulator | 2.1 | x | x | x | x | x | x | x | x | OK |
246252| Nexus One | 2.2 | x | OK | x | OK | x | x | x | x | OK |
247253| HTC Desire | 1.6 | x | OK | x | OK | x | x | x | OK | OK |
248254| Toshiba Folio 100 | 2.2 | x | OK | x | OK | x | x | x | OK | OK |
249255| Xperia X10 | 2.1 | x | OK | x | x | x | x | x | OK | OK |
250256| Xperia X8 | 2.2 | x | OK | x | x | x | x | x | x | OK |
251257| HTC Wildfire | 2.1 | x | OK | x | x | x | x | x | x | OK |
252258| HTC Desire HD | 2.2 | x | OK | x | OK | x | x | x | x | OK |
253259| Galaxy Tab | 2.2 | x | OK | x | x | x | x | x | x | OK |
254260| myPhone A210 | 1.6 | x | OK | x | x | x | x | x | x | OK |
255261| Motorola Milestone | 2.1 | x | OK | x | x | x | x | x | OK | ? |
256262| Milestone 2 | 2.2 | x | OK | x | x | x | x | x | sound-only | OK |
257263
258264## Android file system
259265
260266Android 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\>
261267
262268The native libraries of the application are placed in /data/data/\<package\>/lib
263269
264270A good place where we have read and write access and we can create new files and even run files is /data/data/\<package name\>
265271
266272## Creating a new Java Android application
267273
268274This info can be useful for helping implement LCL-CustomDrawn-Android
269275
270276Generic instructions here: <http://developer.android.com/guide/developing/other-ide.html>
271277
272278### Showing/hiding the virtual keyboard
273279
274280See here: <http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html>
275281
276282To show it:
277283
278284``` pascal
279285InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
280286imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
281287```
282288
283289and for hiding the keyboard:
284290
285291``` pascal
286292InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
287293imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);
288294```
289295
290296## Android JNI
291297
292298Using JNI (Java Native Interface) you can call most Android SDK APIs and also other Java libraries.
293299
294300### Table of Java types and Pascal JNI types
295301
296302All 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.
297303
298304| Java type | VM signature | Type in jni.pas | Pascal equivalent | Description |
299305|----|----|----|----|----|
300306| boolean | Z | jboolean | Boolean | |
301307| byte | B | jbyte | Byte | |
302308| char | C | jchar | Word | |
303309| short | S | jshort | Shortint | |
304310| int | I | jint | Integer | |
305311| long | J | jlong | Int64 | |
306312| float | F | jfloat | Single | |
307313| double | D | jdouble | Double | |
308314| a class, ie: String or InputStream | L\<full class name\> ie: Ljava/lang/String; | jobject | N/A | |
309315| Array, ie: int\[\] | \[\<type-name\> ie: \[I | N/A | | |
310316| A method type | (arg-types)ref-type | JMethodID | N/A | |
311317| void | V | N/A | procedure | |
312318
313319### Example 1: Creating a Java object and calling a Java method
314320
315321Let's say that you want to make Pascal JNI code equivalent to the following Java code:
316322
317323``` java
318324 HttpGet javaRequest = new HttpGet();
319325 URI myURI = new URI("http://magnifier.sourceforge.net/");
320326 javaRequest.setURI(myURI);
321327```
322328
323329First 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:
324330
325331``` pascal
326332uses jni, customdrawnint;
327333
328334var
329335 javaClass_HttpGet, javaClass_URI: jclass;
330336 javaMethod_HttpGet_new, javaMethod_HttpGet_setURI,
331337 javaMethod_URI_new: jmethodid;
332338 javaRequest, javaURI: jobject;
333339 javaString: jstring;
334340 lParams: array[0..2] of JValue;
335341 lNativeString: PChar;
336342begin
337343 DebugLn(':>LoadHTMLPageViaJNI');
338344 // First call FindClass for all required classes
339345 javaClass_HttpGet := javaEnvRef^^.FindClass(javaEnvRef,
340346 'org/apache/http/client/methods/HttpGet');
341347 javaClass_URI := javaEnvRef^^.FindClass(javaEnvRef,
342348 'java/net/URI');
343349
344350 // Now all Method IDs
345351 DebugLn(':LoadHTMLPageViaJNI 1');
346352 javaMethod_HttpGet_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, '<init>', '()V');
347353 javaMethod_HttpGet_setURI := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, 'setURI', '(Ljava/net/URI;)V');
348354 javaMethod_URI_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_URI, '<init>', '(Ljava/lang/String;)V');
349355
350356 // Create a new instance for the HTTP request object
351357 // HttpGet javaRequest = new HttpGet();
352358 javaRequest := javaEnvRef^^.NewObject(javaEnvRef,
353359 javaClass_HttpGet,
354360 javaMethod_HttpGet_new);
355361
356362 DebugLn(':LoadHTMLPageViaJNI 4 javaRequest='+IntToHex(PtrInt(javaRequest), 8));
357363 // Add a URI for the request object
358364 // URI javaURI = new URI("http://magnifier.sourceforge.net/");
359365 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, 'http://magnifier.sourceforge.net');
360366 javaURI := javaEnvRef^^.NewObjectA(javaEnvRef,
361367 javaClass_URI, javaMethod_URI_new, @lParams[0]);
362368 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
363369 // javaRequest.setURI(javaURI);
364370 lParams[0].l := javaURI;
365371 javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaRequest,
366372 javaMethod_HttpGet_setURI, @lParams[0]);
367373```
368374
369375### Example 2: Calling a static method
370376
371377Let'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>
372378
373379``` java
374380 public static native int BASS_StreamCreateFile(String file, long offset, long length, int flags);
375381```
376382
377383First 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:
378384
379385``` pascal
380386uses jni, customdrawnint;
381387
382388var
383389 javaClass_Bass: jclass;
384390 javaMethod_Bass_BASS_StreamCreateFile: jmethodid;
385391 javaString: jstring;
386392 lParams: array[0..3] of JValue;
387393 lRetInt: jint;
388394begin
389395 // First call FindClass for all required classes
390396 javaClass_Bass := javaEnvRef^^.FindClass(javaEnvRef, 'org/jouvieje/bass/Bass');
391397
392398 // Now all Method IDs
393399 javaMethod_Bass_BASS_StreamCreateFile := javaEnvRef^^.GetStaticMethodID(javaEnvRef, javaClass_Bass, 'BASS_StreamCreateFile', '(Ljava/lang/String;JJI)I');
394400
395401 // Make the call
396402 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, '/path/to/file');
397403 lParams[1].j := 0; // offset
398404 lParams[2].j := 200; // length
399405 lParams[3].i := 0; // flags
400406 lRetInt := javaEnvRef^^.CallStaticIntMethodA(javaEnvRef, javaClass_Bass, javaMethod_Bass_BASS_StreamCreateFile, @lParams[0]);
401407 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
402408```
403409
404410## References
405411
406412- <http://developer.android.com/guide/developing/other-ide.html>
407413- [Tutorial "Lazarus and Android"](https://dl.dropboxusercontent.com/u/3753548/Lazarus%20and%20Android.pdf) (PDF file)
408414
409415---
410416*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).*
411417Category: Android/ja

Back to history