Sign in Join
Wiki copytemporary

Android Programming/ja: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11See also [[Custom Drawn Interface/Android/ja]]
22
3-\
4-\
53※今のところ、WindowsでAndroidプログラミングを実際に出来るようにすることを優先目標として記述しています。そのため、原文の翻訳となっていないところが多いです。間違い等はどんどん修正していただけると幸いです。(malcome)\
6-\
74
85Knowing general Android programming can be very useful to help develop the Lazarus Android Interface.
96
107## How to...
118
129### Build the NDK OpenGL example
1310
1411Just follow these steps:
1512
1613**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]]
1714
1815**Step 2** - Install the pre-compiled FPC cross-compiler. Instructions here: [[Android Interface|Android_Interface#Using_the_pre-compiled_compiler]]
1916
2017**Step 3** - Download the latest lazarus-ccr sourceforge code:
2118
2219``` bash
2320svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr lazarus-ccr
2421```
2522
2623or if you think it is too big you can download just the folder lazarus-ccr/bindings/android-ndk\
27-\
24+
2825※ andoroid-ndk バインディングは Lazarus をインスト-ルしたフォルダの、lcl\interfaces\customdrawn\android にもあるのでそれを使えば必要十分であると思います。\
29-\
3026
3127**Step 4** - Building the Pascal Library
3228
3329> **Note:** In the steps below, the directory /home/felipe/Programas or ~/Programas is used. Please adjust if your paths are different.
3430
3531Open the project lazarus-ccr/bindings/android-ndk/examples/opengltest/opengltest.lpi
3632
3733Go to Project-\>Project Options-\>Paths and where is written "Libraries -Fl" you should see a value like this:
3834
3935`/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib`
4036
4137Change 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
4238
4339Now build the project using Lazarus.
4440
4541**Step 5** - Configuring the build environment
4642
4743Open the file opengltest/local.properties. Change this line to point to your SDK location:
4844
4945`sdk.dir=/home/felipe/Programas/android-sdk-linux`
5046
5147**Step 6** - Buildings the APK
5248
5349Open a terminal and type these commands:
5450
5551``` bash
5652cd lazarus-ccr/bindings/ndk/examples/opengltest/android
5753ant debug
5854```
5955
6056The APK file will be placed in opengltest/android/bin/
6157
6258**Step 7** - Install the APK
6359
6460In 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]]
6561
6662The command to install our APK in the phone is:
6763
6864``` bash
6965cd opengltest/android
7066~/Programas/android-sdk-linux/platform-tools/adb install bin/OpenGLNDKTest-debug.apk
7167```
7268
7369You should see something like:
7470
7571`2395 KB/s (107299 bytes in 0.043s)`\
7672`       pkg: /data/local/tmp/OpenGLNDKTest-debug.apk`\
7773`Success`
7874
7975If you get an error message that it is already installed you can install with this command:
8076
8177``` bash
8278~/Programas/android-sdk-linux/platform-tools/adb uninstall com.pascal.opengltest
8379```
8480
8581And then you can run adb logcat to see what the log says while you start it in the phone via its newly added icon:
8682
8783``` bash
8884~/Programas/android-sdk-linux/platform-tools/adb logcat
8985```
9086
9187### Read the screen metrics
9288
9389First fill a TDisplayMetrics with the correct info:
9490
9591``` pascal
9692uses androidutil;
9793
9894var
9995 MyDisplayMetrics: TDisplayMetrics;
10096 Str: string;
10197 //
10298 lHeight, lWidth: Integer;
10399 xdpi, ydpi, lScreenSize: Single;
104100begin
105101 // ..
106102 // Objects
107103
108104 MyDisplayMetrics := TDisplayMetrics.Create;
109105 Activity.getWindowManager().getDefaultDisplay().getMetrics(MyDisplayMetrics);
110106```
111107
112108And then you can read it from the TDisplayMetrics:
113109
114110``` pascal
115111 lHeight := MyDisplayMetrics.heightPixels();
116112 lWidth := MyDisplayMetrics.widthPixels();
117113 xdpi := MyDisplayMetrics.xdpi();
118114 ydpi := MyDisplayMetrics.ydpi();
119115 lScreenSize := sqrt(sqr(lHeight / ydpi) + sqr(lWidth / xdpi));
120116 ldensity := MyDisplayMetrics.density();
121117 ldensityDpi := MyDisplayMetrics.densityDpi();
122118 scaledDensity := MyDisplayMetrics.scaledDensity();
123119```
124120
125121Note 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.
126122
127123## Details about Android devices
128124
129125Since 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).
130126
131127### Smartphones
132128
133129| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
134130|----|----|----|----|----|----|----|
135131| HTC | Wildfire | HTC Wildfire | armv6 | 2.1=\>2.2 | Yes\|\|- | |
136132| Sony Erricson | Xperia X10 | X10i | armv7 | 1.6=\>2.1 | No\|\|- | |
137133
138134### Tablets
139135
140136| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
141137|----|----|----|----|----|----|----|
142138| Toshiba | Folio 100 Tablet | TOSHIBA_FOLIO_AND_A | armv7 | 2.2 | Yes\|\|- | |
143139
144140### List of devices ordered by processor compatibility (ARMv6 vs ARMv7)
145141
146142**List of ARMv7 devices:**
147143
148144- Advent Vega (P10AN01)
149145- Dell Streak, Streak 7
150146- HTC Desire
151147- HTC Desire Z (T-Mobile G2)
152148- HTC Desire HD
153149- HTC Droid Incredible
154150- HTC EVO 4G, EVO Shift 4G
155151- HTC Glacier (T-Mobile myTouch 4G)
156152- HTC Inspire 4G
157153- HTC Nexus One
158154- HTC Thunderbolt 4G
159155- Huawei Ideos S7
160156- LG Optimus Z
161157- Motorola Atrix 4G
162158- Motorola Bravo
163159- Motorola Cliq 2 - untested
164160- Motorola Defy
165161- Motorola Droid 2, Droid 2 Global
166162- Motorola Droid Pro (Motorola PRO)
167163- Motorola Droid X
168164- Motorola Xoom
169165- POV Mobii Tegra Tablet
170166- Samsung Continuum (i400)
171167- Samsung Galaxy S (i9000, Captivate, Fascinate, Vibrant, Epic 4G)
172168- Samsung Galaxy Tab
173169- Sharp IS03
174170- Sony Ericsson Xperia X10
175171- Toshiba AS100
176172- Viewsonic gTablet
177173- Acer Liquid E
178174- Acer Liquid (Liquid A1)
179175- Archos 101 Internet Tablet
180176- Motorola Charm
181177- Motorola Droid (Milestone)
182178- Samsung Galaxy S 4G
183179- Samsung Nexus S
184180
185181**List of ARMv6 processors:**
186182
187183- Android SDK emulator
188184- Asus Garmin nuvifone A50 (T-Mobile Garminfone)
189185- Augen GENTouch 78 Tablet
190186- Coby Kyros Internet Tablet (MID7015)
191187- Geeksphone One, Geeksphone Zero
192188- HTC Aria
193189- HTC ChaCha
194190- HTC Dream (T-Mobile G1, Android Dev Phone 1)
195191- HTC Droid Eris
196192- HTC Espresso (T-Mobile myTouch 3G Slide)
197193- HTC Hero (T-Mobile G2 Touch)
198194- HTC Legend
199195- HTC Magic (T-Mobile myTouch 3G, T-Mobile G1 Touch)
200196- HTC Salsa
201197- HTC Tattoo
202198- HTC Wildfire
203199- Huawei Ascend
204200- Huawei Ideos U8150-B (T-Mobile Comet)
205201- Huawei U8110 (T-Mobile Pulse Mini)
206202- Huawei U8230
207203- LG Ally (Apex) (LG VS740)
208204- LG GW620 (Eve, InTouch Max, LinkMe)
209205- LG Optimus, Optimus M, Optimus T, Optimus S, Optimus V
210206- LG Vortex
211207- LG P500
212208- MAG iMiTO iM7
213209- vMAG iMiTO iM7S
214210- Motorola Backflip
215211- Motorola Citrus
216212- Motorola Cliq (MB200)
217213- Motorola Dext
218214- Motorola Devour
219215- Motorola i1
220216- Motorola Spice XT300
221217- Motorola Quench XT5 XT502
222218- Pandigital Novel
223219- Samsung GT-S5570 Galaxy Mini
224220- Samsung i5500 Galaxy 5 (Corby)
225221- Samsung i5700 Galaxy Portal (Spica)
226222- Samsung i5800 Galaxy 3
227223- Samsung i7500 Galaxy
228224- Samsung Intercept
229225- Samsung M900 Moment
230226- Samsung S5830 Galaxy Ace
231227- Samsung Transform
232228- Sanyo ZIO M6000
233229- Sony Ericsson Xperia X8
234230- Sony Ericsson Xperia X10 Mini
235231- Sony Ericsson Xperia X10 Mini Pro
236232- Superpad 10.2" Tablet PC
237233- Viewsonic ViewPad 7 Tablet
238234- Velocity Micro T103 Cruz tablet
239235- Vodafone 845
240236- ZTE Blade
241237
242238### Playing sounds and videos on the phones
243239
244240Each 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:
245241
246242| 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 |
247243|----|----|----|----|----|----|----|----|----|----|----|
248244| Emulator | 1.6 | x | x | x | x | x | x | x | x | OK |
249245| Emulator | 2.1 | x | x | x | x | x | x | x | x | OK |
250246| Nexus One | 2.2 | x | OK | x | OK | x | x | x | x | OK |
251247| HTC Desire | 1.6 | x | OK | x | OK | x | x | x | OK | OK |
252248| Toshiba Folio 100 | 2.2 | x | OK | x | OK | x | x | x | OK | OK |
253249| Xperia X10 | 2.1 | x | OK | x | x | x | x | x | OK | OK |
254250| Xperia X8 | 2.2 | x | OK | x | x | x | x | x | x | OK |
255251| HTC Wildfire | 2.1 | x | OK | x | x | x | x | x | x | OK |
256252| HTC Desire HD | 2.2 | x | OK | x | OK | x | x | x | x | OK |
257253| Galaxy Tab | 2.2 | x | OK | x | x | x | x | x | x | OK |
258254| myPhone A210 | 1.6 | x | OK | x | x | x | x | x | x | OK |
259255| Motorola Milestone | 2.1 | x | OK | x | x | x | x | x | OK | ? |
260256| Milestone 2 | 2.2 | x | OK | x | x | x | x | x | sound-only | OK |
261257
262258## Android file system
263259
264260Android 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\>
265261
266262The native libraries of the application are placed in /data/data/\<package\>/lib
267263
268264A good place where we have read and write access and we can create new files and even run files is /data/data/\<package name\>
269265
270266## Creating a new Java Android application
271267
272268This info can be useful for helping implement LCL-CustomDrawn-Android
273269
274270Generic instructions here: <http://developer.android.com/guide/developing/other-ide.html>
275271
276272### Showing/hiding the virtual keyboard
277273
278274See here: <http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html>
279275
280276To show it:
281277
282278``` pascal
283279InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
284280imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
285281```
286282
287283and for hiding the keyboard:
288284
289285``` pascal
290286InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
291287imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);
292288```
293289
294290## Android JNI
295291
296292Using JNI (Java Native Interface) you can call most Android SDK APIs and also other Java libraries.
297293
298294### Table of Java types and Pascal JNI types
299295
300296All 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.
301297
302298| Java type | VM signature | Type in jni.pas | Pascal equivalent | Description |
303299|----|----|----|----|----|
304300| boolean | Z | jboolean | Boolean | |
305301| byte | B | jbyte | Byte | |
306302| char | C | jchar | Word | |
307303| short | S | jshort | Shortint | |
308304| int | I | jint | Integer | |
309305| long | J | jlong | Int64 | |
310306| float | F | jfloat | Single | |
311307| double | D | jdouble | Double | |
312308| a class, ie: String or InputStream | L\<full class name\> ie: Ljava/lang/String; | jobject | N/A | |
313309| Array, ie: int\[\] | \[\<type-name\> ie: \[I | N/A | | |
314310| A method type | (arg-types)ref-type | JMethodID | N/A | |
315311| void | V | N/A | procedure | |
316312
317313### Example 1: Creating a Java object and calling a Java method
318314
319315Let's say that you want to make Pascal JNI code equivalent to the following Java code:
320316
321317``` java
322318 HttpGet javaRequest = new HttpGet();
323319 URI myURI = new URI("http://magnifier.sourceforge.net/");
324320 javaRequest.setURI(myURI);
325321```
326322
327323First 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:
328324
329325``` pascal
330326uses jni, customdrawnint;
331327
332328var
333329 javaClass_HttpGet, javaClass_URI: jclass;
334330 javaMethod_HttpGet_new, javaMethod_HttpGet_setURI,
335331 javaMethod_URI_new: jmethodid;
336332 javaRequest, javaURI: jobject;
337333 javaString: jstring;
338334 lParams: array[0..2] of JValue;
339335 lNativeString: PChar;
340336begin
341337 DebugLn(':>LoadHTMLPageViaJNI');
342338 // First call FindClass for all required classes
343339 javaClass_HttpGet := javaEnvRef^^.FindClass(javaEnvRef,
344340 'org/apache/http/client/methods/HttpGet');
345341 javaClass_URI := javaEnvRef^^.FindClass(javaEnvRef,
346342 'java/net/URI');
347343
348344 // Now all Method IDs
349345 DebugLn(':LoadHTMLPageViaJNI 1');
350346 javaMethod_HttpGet_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, '<init>', '()V');
351347 javaMethod_HttpGet_setURI := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, 'setURI', '(Ljava/net/URI;)V');
352348 javaMethod_URI_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_URI, '<init>', '(Ljava/lang/String;)V');
353349
354350 // Create a new instance for the HTTP request object
355351 // HttpGet javaRequest = new HttpGet();
356352 javaRequest := javaEnvRef^^.NewObject(javaEnvRef,
357353 javaClass_HttpGet,
358354 javaMethod_HttpGet_new);
359355
360356 DebugLn(':LoadHTMLPageViaJNI 4 javaRequest='+IntToHex(PtrInt(javaRequest), 8));
361357 // Add a URI for the request object
362358 // URI javaURI = new URI("http://magnifier.sourceforge.net/");
363359 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, 'http://magnifier.sourceforge.net');
364360 javaURI := javaEnvRef^^.NewObjectA(javaEnvRef,
365361 javaClass_URI, javaMethod_URI_new, @lParams[0]);
366362 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
367363 // javaRequest.setURI(javaURI);
368364 lParams[0].l := javaURI;
369365 javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaRequest,
370366 javaMethod_HttpGet_setURI, @lParams[0]);
371367```
372368
373369### Example 2: Calling a static method
374370
375371Let'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>
376372
377373``` java
378374 public static native int BASS_StreamCreateFile(String file, long offset, long length, int flags);
379375```
380376
381377First 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:
382378
383379``` pascal
384380uses jni, customdrawnint;
385381
386382var
387383 javaClass_Bass: jclass;
388384 javaMethod_Bass_BASS_StreamCreateFile: jmethodid;
389385 javaString: jstring;
390386 lParams: array[0..3] of JValue;
391387 lRetInt: jint;
392388begin
393389 // First call FindClass for all required classes
394390 javaClass_Bass := javaEnvRef^^.FindClass(javaEnvRef, 'org/jouvieje/bass/Bass');
395391
396392 // Now all Method IDs
397393 javaMethod_Bass_BASS_StreamCreateFile := javaEnvRef^^.GetStaticMethodID(javaEnvRef, javaClass_Bass, 'BASS_StreamCreateFile', '(Ljava/lang/String;JJI)I');
398394
399395 // Make the call
400396 lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, '/path/to/file');
401397 lParams[1].j := 0; // offset
402398 lParams[2].j := 200; // length
403399 lParams[3].i := 0; // flags
404400 lRetInt := javaEnvRef^^.CallStaticIntMethodA(javaEnvRef, javaClass_Bass, javaMethod_Bass_BASS_StreamCreateFile, @lParams[0]);
405401 javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
406402```
407403
408404## References
409405
410406- <http://developer.android.com/guide/developing/other-ide.html>
411407- [Tutorial "Lazarus and Android"](https://dl.dropboxusercontent.com/u/3753548/Lazarus%20and%20Android.pdf) (PDF file)
412408
413409---
414410*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).*
415411Category: Android/ja

Back to history