Android4Pascal: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Android4Pascal
| Old | New | Line | |
|---|---|---|---|
| 1 | + | *(Android only)* | |
| 2 | + | ||
| 3 | + | Android4Pascal are the bindings which connect Pascal applications running as executables in Android with the Android Java APIs. | |
| 4 | + | ||
| 5 | + | ## Download | |
| 6 | + | ||
| 7 | + | ### Latest development version | |
| 8 | + | ||
| 9 | + | - svn co <https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/bindings/android-sdk> android-sdk | |
| 10 | + | - svn co <https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/examples/androidlcl> androidlcl | |
| 11 | + | ||
| 12 | + | Changelog: | |
| 13 | + | ||
| 14 | + | - Added a tool to automatically generate the bindings | |
| 15 | + | - Added many new APIs (don't remember all of them) | |
| 16 | + | ||
| 17 | + | ### Version 0.3 | |
| 18 | + | ||
| 19 | + | Released June 2011 | |
| 20 | + | ||
| 21 | + | svn co <https://p-tools.svn.sourceforge.net/svnroot/p-tools/systeminfo4android/> systeminfo4android | |
| 22 | + | ||
| 23 | + | Changelog: | |
| 24 | + | ||
| 25 | + | - Added support for reading android.util.DisplayMetrics, Activity.getWindowManager, WindowManager.getDefaultDisplay, Display.getMetrics | |
| 26 | + | ||
| 27 | + | ### Version 0.2 | |
| 28 | + | ||
| 29 | + | Released on March 2011 | |
| 30 | + | ||
| 31 | + | This version was distributed as an example project which can be obtained with the following svn command: | |
| 32 | + | ||
| 33 | + | svn co <https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android/> -r 230 turbochessclock4android | |
| 34 | + | ||
| 35 | + | #### Android API Hello World in Pascal | |
| 36 | + | ||
| 37 | + | Here is an example Pascal application written for Android. It shows how to create controls, receive callback events and how to use the timer. | |
| 38 | + | ||
| 39 | + | The full directory structure can be download with this svn command: | |
| 40 | + | ||
| 41 | + | ` svn co `[`https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android`](https://p-tools.svn.sourceforge.net/svnroot/p-tools/turbochessclock4android)` turbochessclock4android` | |
| 42 | + | ||
| 43 | + | <figure> | |
| 44 | + | <img src="Simple_Android_app.png" title="Simple_Android_app.png" /> | |
| 45 | + | <figcaption>Simple_Android_app.png</figcaption> | |
| 46 | + | </figure> | |
| 47 | + | ||
| 48 | + | Here is the Pascal code from this example: | |
| 49 | + | ||
| 50 | + | ``` pascal | |
| 51 | + | { | |
| 52 | + | A simple Chess Clock application | |
| 53 | + | ||
| 54 | + | Author: Felipe Monteiro de Carvalho - 2011 | |
| 55 | + | License: Public Domain | |
| 56 | + | } | |
| 57 | + | program turbochessclock4android; | |
| 58 | + | ||
| 59 | + | {$mode objfpc}{$H+} | |
| 60 | + | ||
| 61 | + | uses | |
| 62 | + | Classes, SysUtils, androidpipescomm, androidview, javalang, | |
| 63 | + | androidapp, androidtimer, androidutil; | |
| 64 | + | ||
| 65 | + | type | |
| 66 | + | TEventHandler = class | |
| 67 | + | public | |
| 68 | + | procedure HandleOnTimer(ASender: TObject); | |
| 69 | + | procedure buttonStartClickCallback(v: TView); | |
| 70 | + | procedure buttonMoveClickCallback(v: TView); | |
| 71 | + | end; | |
| 72 | + | ||
| 73 | + | var | |
| 74 | + | layout: TLinearLayout; | |
| 75 | + | params: TLayoutParams; | |
| 76 | + | tv, black_label, white_label: TTextView; | |
| 77 | + | scroller: TScrollView; | |
| 78 | + | btn_move, btn_start: TButton; | |
| 79 | + | tp: TTimePicker; | |
| 80 | + | WhiteTimeCount: Integer = 0; | |
| 81 | + | BlackTimeCount: Integer = 0; | |
| 82 | + | MyTimer: TAndroidTimer; | |
| 83 | + | MyEventHandler: TEventHandler; | |
| 84 | + | IsWhitePlayerMove: Boolean = True; | |
| 85 | + | ||
| 86 | + | procedure TEventHandler.buttonStartClickCallback(v: TView); | |
| 87 | + | begin | |
| 88 | + | black_label.setVisibility(VISIBLE); | |
| 89 | + | white_label.setVisibility(VISIBLE); | |
| 90 | + | btn_move.setVisibility(VISIBLE); | |
| 91 | + | // | |
| 92 | + | WhiteTimeCount := tp.getCurrentHour() * 60 * 60 + tp.getCurrentMinute() * 60; | |
| 93 | + | BlackTimeCount := WhiteTimeCount; | |
| 94 | + | // | |
| 95 | + | MyTimer.removeCallbacks(); | |
| 96 | + | MyTimer.postDelayed(100); | |
| 97 | + | end; | |
| 98 | + | ||
| 99 | + | procedure TEventHandler.buttonMoveClickCallback(v: TView); | |
| 100 | + | begin | |
| 101 | + | IsWhitePlayerMove := not IsWhitePlayerMove; | |
| 102 | + | end; | |
| 103 | + | ||
| 104 | + | procedure TEventHandler.HandleOnTimer(ASender: TObject); | |
| 105 | + | var | |
| 106 | + | lSeconds, lMinutes, lHours: Integer; | |
| 107 | + | begin | |
| 108 | + | if IsWhitePlayerMove then | |
| 109 | + | begin | |
| 110 | + | lSeconds := WhiteTimeCount mod 60; | |
| 111 | + | lMinutes := (WhiteTimeCount mod (60 * 60)) div 60; | |
| 112 | + | lHours := WhiteTimeCount div (60 * 60); | |
| 113 | + | white_label.setText(Format('White %d:%d:%d', [lHours, lMinutes, lSeconds])); | |
| 114 | + | // | |
| 115 | + | if WhiteTimeCount = 0 then Exit; | |
| 116 | + | Dec(WhiteTimeCount); | |
| 117 | + | end | |
| 118 | + | else | |
| 119 | + | begin | |
| 120 | + | lSeconds := BlackTimeCount mod 60; | |
| 121 | + | lMinutes := (BlackTimeCount mod (60 * 60)) div 60; | |
| 122 | + | lHours := BlackTimeCount div (60 * 60); | |
| 123 | + | black_label.setText(Format('Black %d:%d:%d', [lHours, lMinutes, lSeconds])); | |
| 124 | + | // | |
| 125 | + | if BlackTimeCount = 0 then Exit; | |
| 126 | + | Dec(BlackTimeCount); | |
| 127 | + | end; | |
| 128 | + | // | |
| 129 | + | MyTimer.removeCallbacks(); | |
| 130 | + | // Note that this has a low precision, but it enough for this simple app | |
| 131 | + | // A more precise app would keep another time count instead of just using Dec | |
| 132 | + | // at each timer call | |
| 133 | + | MyTimer.postDelayed(1000); | |
| 134 | + | end; | |
| 135 | + | ||
| 136 | + | begin | |
| 137 | + | // Here add any initialization. | |
| 138 | + | // Any initialization code will be run inside Activity.onCreate, | |
| 139 | + | // so keep it as short as possible! | |
| 140 | + | // It should mostly contain GUI initialization | |
| 141 | + | // User interface | |
| 142 | + | MyEventHandler := TEventHandler.Create; | |
| 143 | + | ||
| 144 | + | // Prepares the UI of the program | |
| 145 | + | layout := TLinearLayout.Create; | |
| 146 | + | params := TLayoutParams.Create(androidview.FILL_PARENT, androidview.FILL_PARENT); | |
| 147 | + | layout.setLayoutParams(params); | |
| 148 | + | params.Free; | |
| 149 | + | layout.setOrientation(androidview.VERTICAL); | |
| 150 | + | ||
| 151 | + | // Game UI | |
| 152 | + | ||
| 153 | + | black_label := TTextView.Create; | |
| 154 | + | black_label.setText('Black time:'); | |
| 155 | + | black_label.setVisibility(GONE); | |
| 156 | + | black_label.setTextSize(COMPLEX_UNIT_PX, 40); | |
| 157 | + | layout.addView(black_label); | |
| 158 | + | ||
| 159 | + | white_label := TTextView.Create; | |
| 160 | + | white_label.setText('White time:'); | |
| 161 | + | white_label.setVisibility(GONE); | |
| 162 | + | white_label.setTextSize(COMPLEX_UNIT_PX, 40); | |
| 163 | + | layout.addView(white_label); | |
| 164 | + | ||
| 165 | + | btn_move := TButton.Create; | |
| 166 | + | btn_move.setText('Move finished!'); | |
| 167 | + | btn_move.setOnClickListener(@MyEventHandler.buttonMoveClickCallback); | |
| 168 | + | btn_move.setVisibility(GONE); | |
| 169 | + | layout.addView(btn_move); | |
| 170 | + | ||
| 171 | + | // Config UI | |
| 172 | + | ||
| 173 | + | tv := TTextView.Create; | |
| 174 | + | tv.setText('Please select how much to give to the players and press "Start Game":'); | |
| 175 | + | layout.addView(tv); | |
| 176 | + | ||
| 177 | + | tp := TTimePicker.Create; | |
| 178 | + | tp.setIs24HourView(True); | |
| 179 | + | tp.setCurrentHour(0); | |
| 180 | + | tp.setCurrentMinute(30); | |
| 181 | + | layout.addView(tp); | |
| 182 | + | ||
| 183 | + | btn_start := TButton.Create; | |
| 184 | + | btn_start.setText('Start game!'); | |
| 185 | + | btn_start.setOnClickListener(@MyEventHandler.buttonStartClickCallback); | |
| 186 | + | layout.addView(btn_start); | |
| 187 | + | ||
| 188 | + | // And also allow the user to scroll the UI if it is larger then the screen width | |
| 189 | + | // Scrolling takes place only horizontally | |
| 190 | + | ||
| 191 | + | scroller := TScrollView.Create; | |
| 192 | + | scroller.addView(layout); | |
| 193 | + | ||
| 194 | + | Activity.setContentView(scroller); | |
| 195 | + | ||
| 196 | + | MyTimer := TAndroidTimer.Create; | |
| 197 | + | MyTimer.OnTimer := @MyEventHandler.HandleOnTimer; | |
| 198 | + | ||
| 199 | + | // Now tell Java that the initialization has finished | |
| 200 | + | vAndroidPipesComm.onCreateFinished(); | |
| 201 | + | // Here you can add any other initialization, | |
| 202 | + | // specially non-GUI code | |
| 203 | + | ||
| 204 | + | // Now we block our execution waiting for callbacks from Java | |
| 205 | + | vAndroidPipesComm.MessageLoop(); | |
| 206 | + | end. | |
| 207 | + | ``` | |
| 208 | + | ||
| 209 | + | ##### Compiling the example project in Linux | |
| 210 | + | ||
| 211 | + | 1\> Get a working arm-linux cross-compiler which generates ARMv5 eabi with softfloat (as many phones like HTC Wildfire have no FPU) | |
| 212 | + | ||
| 213 | + | To do this one can download an unofficial build of FPC 2.5.1 from here: | |
| 214 | + | ||
| 215 | + | <http://sourceforge.net/projects/p-tools/files/Free%20Pascal%20for%20ARM/> Compiled for ARMv5 eabi with softfloat. | |
| 216 | + | ||
| 217 | + | Or an older FPC 2.4.2 | |
| 218 | + | ||
| 219 | + | <http://members.yline.com/~tom_at_work/fpc-2.4.2.UNOFFICIAL.arm-linux.tar> Compiled for ARMv5 eabi with softfloat. | |
| 220 | + | ||
| 221 | + | Or build your own. There are instructions here: [[Setup Cross Compile For ARM]] | |
| 222 | + | ||
| 223 | + | 2\> Install the Android SDK. Instructions here: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Using_the_Android_SDK]] | |
| 224 | + | ||
| 225 | + | 3\> Install ant, for example in Mandriva Linux: | |
| 226 | + | ||
| 227 | + | ` urpmi ant` | |
| 228 | + | ||
| 229 | + | 4\> Open the project turbochessclock/turbochessclock4android.lpi in Lazarus and build it | |
| 230 | + | ||
| 231 | + | 5\> Build the APK file in debug mode | |
| 232 | + | ||
| 233 | + | ` ant debug` | |
| 234 | + | ||
| 235 | + | 6\> Connect your phone and make sure you can connect to it via ADB. More info here: [[Android Interface/Using the Android SDK, Emulator and Phones|Android_Interface/Using_the_Android_SDK%2C_Emulator_and_Phones#Recognition_of_devices_under_Linux]] | |
| 236 | + | ||
| 237 | + | 7\> Install the APK file in your phone via ADB or whatever other method you prefer: | |
| 238 | + | ||
| 239 | + | ` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk` | |
| 240 | + | ||
| 241 | + | If the package is already installed you need need to do this instead: | |
| 242 | + | ||
| 243 | + | ` ../android-sdk-linux_x86/tools/adb uninstall com.pascal.turbochessclock`\ | |
| 244 | + | ` ../android-sdk-linux_x86/tools/adb install android/bin/TurboChessClock4Android-debug.apk` | |
| 245 | + | ||
| 246 | + | ### Version 0.1 | |
| 247 | + | ||
| 248 | + | Released on January 2011 | |
| 249 | + | ||
| 250 | + | This version was distributed as an example project which can be obtained with the following svn command: | |
| 251 | + | ||
| 252 | + | svn co <https://p-tools.svn.sourceforge.net/svnroot/PascalNotes4Android/> -r 230 PascalNotes4Android | |
| 253 | + | ||
| 254 | + | ## See also | |
| 255 | + | ||
| 256 | + | - [[Android Interface/Native Android GUI]] | |
| 257 | + | - [[Android Programming]] | |
| 258 | + | ||
| 259 | + | --- | |
| 260 | + | *From the Free Pascal wiki, page "Android4Pascal", 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).* | |
| 261 | + | Category: Android |
