I ported my JavaFX application to my Android device. I want my application to read incoming SMS messages and store it in a database. I found several questions here in StackOverflow but I don't know how to implement in a JavaFX approach. Please help!
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
These are the required steps to create and port a JavaFX application to an Android device, so you can track SMS messages, allowing:
Step 1
Using Gluon plugin for NetBeans create a new JavaFX project. Let's call it SMSTracker, with main class
org.jpereda.sms.SMSTrackerFX
. Onbuild.gradle
, update the jfxmobile plugin version to b9:First of all let's create
SMSMessage
, a JavaFX pojo with our model:With ScenicBuilder and FXML or by code, create your UI. For this sample, it will be a simple UI with three main areas, for the three above mentioned options.
And this is what we have for now:
Step 2
Following HelloPlatform sample, we will add a
PlatformService
class and aPlatformProvider
interface, with the required services:This interface will be implemented on each of the three platforms (desktop, iOS and Android). Obviously, we'll focus only on the Android implementation.
This is the project view on NetBeans with all the packages and files involved:
Step 3
Let's implement now the services on Android. For that I've followed several great answers at SO: sending SMS, read inbox and listening to incoming SMS.
From the last one, we can crete a class that sets an
ObjectProperty
with anSMSMessage
object every time one is received:In order to launch this listener, we use
FXActivity
, the class that extends the Android Context class and provides the access to the Android services, to register an instance ofSmsListener
:Step 4
Finally, all we need to do is bind the property with our label on the UI and start the broadcast:
Note the use of
Platform.runLater()
to update the label: the broadcast thread is different than the JavaFX thread.Step 5
The last step before building the apk consists on modifying the
AndroidManifest.xml
file to ask for the required permissions.Since the jfxmobile-plugin creates one by default, running
gradlew android
at this stage will generate it. It is located underSMSTracker\build\javafxports\tmp\android
folder.Copy it to another location (
SMSTracker\lib
) and include its reference in thebuild.gradle
file:Now edit the file and add the required permissions and the receiver:
Save, build and run
gradlew androidInstall
to upload the apk to your device.Full project
All the code for this application can be found here, including apk ready to be installed on Android devices.