Intro
When it comes to Cross-Platform application development frameworks, you get all the tools you need to create whatever you want. You can shape that framework and create new development tools for yourself by using or expanding the framework itself. Take the FireMonkey framework, for example. If you don’t have access to third-party APIs, you can convert them into Delphi or C++ Builder units. The FireMonkey provides you with essential helper units to help you expand it yourself.
To automate the conversion process, you can utilize the JavaImport for the Android tool by WINSOFT. This tool is commercial, and there is a trial version that can convert .jar or .class files into usable Delphi & C++ Builder units. But there is a limit. If you would like to turn Android APIs into Delphi or C++ Builder, you need to get a license.
Specifications
- Supported OSs: Android 32 & Android 64
- Available for Delphi & C++ Builder 6-10.4
How to use it?
We recommend that you watch our action video about this tool, which shows how to convert .class file into .pas file with one command. It also demonstrates the result of the generated files in a real application.
Let’s investigate how we can use the converted android.widget.Toast API. As an example, there is the android.widget.Toast.pas unit. First, you should add the module to the uses list.
As you can see, everything is converted to Object Pascal code.
Here is the implementation:
1 2 3 4 5 6 7 8 9 10 11 12 |
procedure ShowToast(const Message: string); overload; begin {$if CompilerVersion < 32} CallInUiThread( procedure begin {$endif} TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(Message), TJToast_LENGTH_SHORT).show; {$if CompilerVersion < 32} end); {$endif} end; |
If you pay attention to the code, there is compiler level conditional if the compiler version is less than 32, which means not Sydney, Rio, and Tokyo versions, it executed code in using the CallInUiThread procedure. This procedure is deprecated now, and this was an essential procedure to handle launching activities and handling results. In newer versions, all code is currently running in the Java UI Thread, removing the need for thread synchronization.
Then we are calling the TJToast with JavaClass, which bridges the units. And also, you should always pay attention to the type of conversion.
Next, with the help of ShowToast, you’ll learn how to get battery level from your FireMonkey Android project.
1 2 3 4 5 6 7 8 9 10 11 12 |
procedure TFormMain.ButtonBatteryClick(Sender: TObject); var Filter: JIntentFilter; Battery: JIntent; Level, Scale: Integer; begin Filter := TJIntentFilter.JavaClass.init(TJIntent.JavaClass.ACTION_BATTERY_CHANGED); Battery := MainActivity.registerReceiver(nil, filter); Level := Battery.getIntExtra(TJBatteryManager.JavaClass.EXTRA_LEVEL, -1); Scale := battery.getIntExtra(TJBatteryManager.JavaClass.EXTRA_SCALE, -1); ShowToast('Battery level: ' + FloatToStr(100 * Level / Scale), TJGravity_CENTER_HORIZONTAL or TJGravity_CENTER_VERTICAL); end; |
- First, bridging the TJIntentFilter interface.
- Registering the receiver using the JFMXNativeActivity
- Then takes the EXTRA_LEVEL and EXTRA_SCALE properties
- Using the ShowToast displays the battery level.
Remember to check out the demonstration video above.
Head over and check out the full WINSOFT JavaImport tool.
Are you interested in converting android APIs in your android projects with the Cross-Platform Development tool, which will assist you in developing apps in the Delphi or C++ environments?
You can get the JavaImport Tool and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition