Sign in Join
Wiki copytemporary

Android Programming/ja: what changed

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

Title: Android Programming/ja

OldNewLine
1+See also [[Custom Drawn Interface/Android/ja]]
2+
3+\
4+\
5+※今のところ、WindowsでAndroidプログラミングを実際に出来るようにすることを優先目標として記述しています。そのため、原文の翻訳となっていないところが多いです。間違い等はどんどん修正していただけると幸いです。(malcome)\
6+\
7+
8+Knowing general Android programming can be very useful to help develop the Lazarus Android Interface.
9+
10+## How to...
11+
12+### Build the NDK OpenGL example
13+
14+Just follow these steps:
15+
16+**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]]
17+
18+**Step 2** - Install the pre-compiled FPC cross-compiler. Instructions here: [[Android Interface|Android_Interface#Using_the_pre-compiled_compiler]]
19+
20+**Step 3** - Download the latest lazarus-ccr sourceforge code:
21+
22+``` bash
23+svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr lazarus-ccr
24+```
25+
26+or if you think it is too big you can download just the folder lazarus-ccr/bindings/android-ndk\
27+\
28+※ andoroid-ndk バインディングは Lazarus をインスト-ルしたフォルダの、lcl\interfaces\customdrawn\android にもあるのでそれを使えば必要十分であると思います。\
29+\
30+
31+**Step 4** - Building the Pascal Library
32+
33+> **Note:** In the steps below, the directory /home/felipe/Programas or ~/Programas is used. Please adjust if your paths are different.
34+
35+Open the project lazarus-ccr/bindings/android-ndk/examples/opengltest/opengltest.lpi
36+
37+Go to Project-\>Project Options-\>Paths and where is written "Libraries -Fl" you should see a value like this:
38+
39+`/home/felipe/Programas/android-ndk-r5/platforms/android-9/arch-arm/usr/lib`
40+
41+Change 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
42+
43+Now build the project using Lazarus.
44+
45+**Step 5** - Configuring the build environment
46+
47+Open the file opengltest/local.properties. Change this line to point to your SDK location:
48+
49+`sdk.dir=/home/felipe/Programas/android-sdk-linux`
50+
51+**Step 6** - Buildings the APK
52+
53+Open a terminal and type these commands:
54+
55+``` bash
56+cd lazarus-ccr/bindings/ndk/examples/opengltest/android
57+ant debug
58+```
59+
60+The APK file will be placed in opengltest/android/bin/
61+
62+**Step 7** - Install the APK
63+
64+In 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]]
65+
66+The command to install our APK in the phone is:
67+
68+``` bash
69+cd opengltest/android
70+~/Programas/android-sdk-linux/platform-tools/adb install bin/OpenGLNDKTest-debug.apk
71+```
72+
73+You should see something like:
74+
75+`2395 KB/s (107299 bytes in 0.043s)`\
76+`       pkg: /data/local/tmp/OpenGLNDKTest-debug.apk`\
77+`Success`
78+
79+If you get an error message that it is already installed you can install with this command:
80+
81+``` bash
82+~/Programas/android-sdk-linux/platform-tools/adb uninstall com.pascal.opengltest
83+```
84+
85+And then you can run adb logcat to see what the log says while you start it in the phone via its newly added icon:
86+
87+``` bash
88+~/Programas/android-sdk-linux/platform-tools/adb logcat
89+```
90+
91+### Read the screen metrics
92+
93+First fill a TDisplayMetrics with the correct info:
94+
95+``` pascal
96+uses androidutil;
97+
98+var
99+ MyDisplayMetrics: TDisplayMetrics;
100+ Str: string;
101+ //
102+ lHeight, lWidth: Integer;
103+ xdpi, ydpi, lScreenSize: Single;
104+begin
105+ // ..
106+ // Objects
107+
108+ MyDisplayMetrics := TDisplayMetrics.Create;
109+ Activity.getWindowManager().getDefaultDisplay().getMetrics(MyDisplayMetrics);
110+```
111+
112+And then you can read it from the TDisplayMetrics:
113+
114+``` pascal
115+ lHeight := MyDisplayMetrics.heightPixels();
116+ lWidth := MyDisplayMetrics.widthPixels();
117+ xdpi := MyDisplayMetrics.xdpi();
118+ ydpi := MyDisplayMetrics.ydpi();
119+ lScreenSize := sqrt(sqr(lHeight / ydpi) + sqr(lWidth / xdpi));
120+ ldensity := MyDisplayMetrics.density();
121+ ldensityDpi := MyDisplayMetrics.densityDpi();
122+ scaledDensity := MyDisplayMetrics.scaledDensity();
123+```
124+
125+Note 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.
126+
127+## Details about Android devices
128+
129+Since 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).
130+
131+### Smartphones
132+
133+| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
134+|----|----|----|----|----|----|----|
135+| HTC | Wildfire | HTC Wildfire | armv6 | 2.1=\>2.2 | Yes\|\|- | |
136+| Sony Erricson | Xperia X10 | X10i | armv7 | 1.6=\>2.1 | No\|\|- | |
137+
138+### Tablets
139+
140+| Manufacturer | Model | Android API Name (Build.Model) | Processor | Android version | Multi-touch | Comments |
141+|----|----|----|----|----|----|----|
142+| Toshiba | Folio 100 Tablet | TOSHIBA_FOLIO_AND_A | armv7 | 2.2 | Yes\|\|- | |
143+
144+### List of devices ordered by processor compatibility (ARMv6 vs ARMv7)
145+
146+**List of ARMv7 devices:**
147+
148+- Advent Vega (P10AN01)
149+- Dell Streak, Streak 7
150+- HTC Desire
151+- HTC Desire Z (T-Mobile G2)
152+- HTC Desire HD
153+- HTC Droid Incredible
154+- HTC EVO 4G, EVO Shift 4G
155+- HTC Glacier (T-Mobile myTouch 4G)
156+- HTC Inspire 4G
157+- HTC Nexus One
158+- HTC Thunderbolt 4G
159+- Huawei Ideos S7
160+- LG Optimus Z
161+- Motorola Atrix 4G
162+- Motorola Bravo
163+- Motorola Cliq 2 - untested
164+- Motorola Defy
165+- Motorola Droid 2, Droid 2 Global
166+- Motorola Droid Pro (Motorola PRO)
167+- Motorola Droid X
168+- Motorola Xoom
169+- POV Mobii Tegra Tablet
170+- Samsung Continuum (i400)
171+- Samsung Galaxy S (i9000, Captivate, Fascinate, Vibrant, Epic 4G)
172+- Samsung Galaxy Tab
173+- Sharp IS03
174+- Sony Ericsson Xperia X10
175+- Toshiba AS100
176+- Viewsonic gTablet
177+- Acer Liquid E
178+- Acer Liquid (Liquid A1)
179+- Archos 101 Internet Tablet
180+- Motorola Charm
181+- Motorola Droid (Milestone)
182+- Samsung Galaxy S 4G
183+- Samsung Nexus S
184+
185+**List of ARMv6 processors:**
186+
187+- Android SDK emulator
188+- Asus Garmin nuvifone A50 (T-Mobile Garminfone)
189+- Augen GENTouch 78 Tablet
190+- Coby Kyros Internet Tablet (MID7015)
191+- Geeksphone One, Geeksphone Zero
192+- HTC Aria
193+- HTC ChaCha
194+- HTC Dream (T-Mobile G1, Android Dev Phone 1)
195+- HTC Droid Eris
196+- HTC Espresso (T-Mobile myTouch 3G Slide)
197+- HTC Hero (T-Mobile G2 Touch)
198+- HTC Legend
199+- HTC Magic (T-Mobile myTouch 3G, T-Mobile G1 Touch)
200+- HTC Salsa
201+- HTC Tattoo
202+- HTC Wildfire
203+- Huawei Ascend
204+- Huawei Ideos U8150-B (T-Mobile Comet)
205+- Huawei U8110 (T-Mobile Pulse Mini)
206+- Huawei U8230
207+- LG Ally (Apex) (LG VS740)
208+- LG GW620 (Eve, InTouch Max, LinkMe)
209+- LG Optimus, Optimus M, Optimus T, Optimus S, Optimus V
210+- LG Vortex
211+- LG P500
212+- MAG iMiTO iM7
213+- vMAG iMiTO iM7S
214+- Motorola Backflip
215+- Motorola Citrus
216+- Motorola Cliq (MB200)
217+- Motorola Dext
218+- Motorola Devour
219+- Motorola i1
220+- Motorola Spice XT300
221+- Motorola Quench XT5 XT502
222+- Pandigital Novel
223+- Samsung GT-S5570 Galaxy Mini
224+- Samsung i5500 Galaxy 5 (Corby)
225+- Samsung i5700 Galaxy Portal (Spica)
226+- Samsung i5800 Galaxy 3
227+- Samsung i7500 Galaxy
228+- Samsung Intercept
229+- Samsung M900 Moment
230+- Samsung S5830 Galaxy Ace
231+- Samsung Transform
232+- Sanyo ZIO M6000
233+- Sony Ericsson Xperia X8
234+- Sony Ericsson Xperia X10 Mini
235+- Sony Ericsson Xperia X10 Mini Pro
236+- Superpad 10.2" Tablet PC
237+- Viewsonic ViewPad 7 Tablet
238+- Velocity Micro T103 Cruz tablet
239+- Vodafone 845
240+- ZTE Blade
241+
242+### Playing sounds and videos on the phones
243+
244+Each 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:
245+
246+| 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 |
247+|----|----|----|----|----|----|----|----|----|----|----|
248+| Emulator | 1.6 | x | x | x | x | x | x | x | x | OK |
249+| Emulator | 2.1 | x | x | x | x | x | x | x | x | OK |
250+| Nexus One | 2.2 | x | OK | x | OK | x | x | x | x | OK |
251+| HTC Desire | 1.6 | x | OK | x | OK | x | x | x | OK | OK |
252+| Toshiba Folio 100 | 2.2 | x | OK | x | OK | x | x | x | OK | OK |
253+| Xperia X10 | 2.1 | x | OK | x | x | x | x | x | OK | OK |
254+| Xperia X8 | 2.2 | x | OK | x | x | x | x | x | x | OK |
255+| HTC Wildfire | 2.1 | x | OK | x | x | x | x | x | x | OK |
256+| HTC Desire HD | 2.2 | x | OK | x | OK | x | x | x | x | OK |
257+| Galaxy Tab | 2.2 | x | OK | x | x | x | x | x | x | OK |
258+| myPhone A210 | 1.6 | x | OK | x | x | x | x | x | x | OK |
259+| Motorola Milestone | 2.1 | x | OK | x | x | x | x | x | OK | ? |
260+| Milestone 2 | 2.2 | x | OK | x | x | x | x | x | sound-only | OK |
261+
262+## Android file system
263+
264+Android 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\>
265+
266+The native libraries of the application are placed in /data/data/\<package\>/lib
267+
268+A good place where we have read and write access and we can create new files and even run files is /data/data/\<package name\>
269+
270+## Creating a new Java Android application
271+
272+This info can be useful for helping implement LCL-CustomDrawn-Android
273+
274+Generic instructions here: <http://developer.android.com/guide/developing/other-ide.html>
275+
276+### Showing/hiding the virtual keyboard
277+
278+See here: <http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html>
279+
280+To show it:
281+
282+``` pascal
283+InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
284+imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
285+```
286+
287+and for hiding the keyboard:
288+
289+``` pascal
290+InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
291+imm.hideSoftInputFromWindow(singleedittext.getWindowToken(),0);
292+```
293+
294+## Android JNI
295+
296+Using JNI (Java Native Interface) you can call most Android SDK APIs and also other Java libraries.
297+
298+### Table of Java types and Pascal JNI types
299+
300+All 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.
301+
302+| Java type | VM signature | Type in jni.pas | Pascal equivalent | Description |
303+|----|----|----|----|----|
304+| boolean | Z | jboolean | Boolean | |
305+| byte | B | jbyte | Byte | |
306+| char | C | jchar | Word | |
307+| short | S | jshort | Shortint | |
308+| int | I | jint | Integer | |
309+| long | J | jlong | Int64 | |
310+| float | F | jfloat | Single | |
311+| double | D | jdouble | Double | |
312+| a class, ie: String or InputStream | L\<full class name\> ie: Ljava/lang/String; | jobject | N/A | |
313+| Array, ie: int\[\] | \[\<type-name\> ie: \[I | N/A | | |
314+| A method type | (arg-types)ref-type | JMethodID | N/A | |
315+| void | V | N/A | procedure | |
316+
317+### Example 1: Creating a Java object and calling a Java method
318+
319+Let's say that you want to make Pascal JNI code equivalent to the following Java code:
320+
321+``` java
322+ HttpGet javaRequest = new HttpGet();
323+ URI myURI = new URI("http://magnifier.sourceforge.net/");
324+ javaRequest.setURI(myURI);
325+```
326+
327+First 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:
328+
329+``` pascal
330+uses jni, customdrawnint;
331+
332+var
333+ javaClass_HttpGet, javaClass_URI: jclass;
334+ javaMethod_HttpGet_new, javaMethod_HttpGet_setURI,
335+ javaMethod_URI_new: jmethodid;
336+ javaRequest, javaURI: jobject;
337+ javaString: jstring;
338+ lParams: array[0..2] of JValue;
339+ lNativeString: PChar;
340+begin
341+ DebugLn(':>LoadHTMLPageViaJNI');
342+ // First call FindClass for all required classes
343+ javaClass_HttpGet := javaEnvRef^^.FindClass(javaEnvRef,
344+ 'org/apache/http/client/methods/HttpGet');
345+ javaClass_URI := javaEnvRef^^.FindClass(javaEnvRef,
346+ 'java/net/URI');
347+
348+ // Now all Method IDs
349+ DebugLn(':LoadHTMLPageViaJNI 1');
350+ javaMethod_HttpGet_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, '<init>', '()V');
351+ javaMethod_HttpGet_setURI := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_HttpGet, 'setURI', '(Ljava/net/URI;)V');
352+ javaMethod_URI_new := javaEnvRef^^.GetMethodID(javaEnvRef, javaClass_URI, '<init>', '(Ljava/lang/String;)V');
353+
354+ // Create a new instance for the HTTP request object
355+ // HttpGet javaRequest = new HttpGet();
356+ javaRequest := javaEnvRef^^.NewObject(javaEnvRef,
357+ javaClass_HttpGet,
358+ javaMethod_HttpGet_new);
359+
360+ DebugLn(':LoadHTMLPageViaJNI 4 javaRequest='+IntToHex(PtrInt(javaRequest), 8));
361+ // Add a URI for the request object
362+ // URI javaURI = new URI("http://magnifier.sourceforge.net/");
363+ lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, 'http://magnifier.sourceforge.net');
364+ javaURI := javaEnvRef^^.NewObjectA(javaEnvRef,
365+ javaClass_URI, javaMethod_URI_new, @lParams[0]);
366+ javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
367+ // javaRequest.setURI(javaURI);
368+ lParams[0].l := javaURI;
369+ javaEnvRef^^.CallVoidMethodA(javaEnvRef, javaRequest,
370+ javaMethod_HttpGet_setURI, @lParams[0]);
371+```
372+
373+### Example 2: Calling a static method
374+
375+Let'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>
376+
377+``` java
378+ public static native int BASS_StreamCreateFile(String file, long offset, long length, int flags);
379+```
380+
381+First 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:
382+
383+``` pascal
384+uses jni, customdrawnint;
385+
386+var
387+ javaClass_Bass: jclass;
388+ javaMethod_Bass_BASS_StreamCreateFile: jmethodid;
389+ javaString: jstring;
390+ lParams: array[0..3] of JValue;
391+ lRetInt: jint;
392+begin
393+ // First call FindClass for all required classes
394+ javaClass_Bass := javaEnvRef^^.FindClass(javaEnvRef, 'org/jouvieje/bass/Bass');
395+
396+ // Now all Method IDs
397+ javaMethod_Bass_BASS_StreamCreateFile := javaEnvRef^^.GetStaticMethodID(javaEnvRef, javaClass_Bass, 'BASS_StreamCreateFile', '(Ljava/lang/String;JJI)I');
398+
399+ // Make the call
400+ lParams[0].l := javaEnvRef^^.NewStringUTF(javaEnvRef, '/path/to/file');
401+ lParams[1].j := 0; // offset
402+ lParams[2].j := 200; // length
403+ lParams[3].i := 0; // flags
404+ lRetInt := javaEnvRef^^.CallStaticIntMethodA(javaEnvRef, javaClass_Bass, javaMethod_Bass_BASS_StreamCreateFile, @lParams[0]);
405+ javaEnvRef^^.DeleteLocalRef(javaEnvRef, lParams[0].l);
406+```
407+
408+## References
409+
410+- <http://developer.android.com/guide/developing/other-ide.html>
411+- [Tutorial "Lazarus and Android"](https://dl.dropboxusercontent.com/u/3753548/Lazarus%20and%20Android.pdf) (PDF file)
412+
413+---
414+*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).*
415+Category: Android/ja

Back to history