How to implemented in JAVAFX/Gluon a button to go to the default send Message for my Android Mobile

673 Views Asked by At

I have implemented a Button in my Mobile Application und have a Textfieldwith a Telefon Nummer.

What i want to achieve: When the User Click the Button-> The Default Page to send SMS is open in Android and the Telefonnummer is Paste in the default Nummer Textfield for Android.

I Use Gluon Mobile with JavaFX

:compileJava/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:4: error: package android.content does not exist
import android.content.Intent;
                      ^
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:5: error: package android.net does not exist
import android.net.Uri;
                  ^
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:12: error: cannot find symbol
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);         
        ^
  symbol:   class Intent
  location: class AndroidSMSService
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:12: error: cannot find symbol
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);         
                               ^
  symbol:   class Intent
  location: class AndroidSMSService
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:12: error: cannot find symbol
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);         
                                      ^
  symbol:   variable Intent
  location: class AndroidSMSService
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:13: error: cannot find symbol
        smsIntent.setData(Uri.parse("sms:"));
                          ^
  symbol:   variable Uri
  location: class AndroidSMSService
/Users/yotti/CAMVOYAGE/MultiViewProjectFXML/src/main/java/com/gluonapplication/views/AndroidSMSService.java:15: error: cannot access Activity
        FXActivity.getInstance().startActivity(smsIntent);
                  ^
  class file for android.app.Activity not found
7 errors
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

enter image description here

1

There are 1 best solutions below

16
On BEST ANSWER

I suggest you have a look at how the Charm Down plugins are implemented.

You will see that you can use most of the existing services without further modifications.

If the service you are looking for is not implemented yet, it is really easy to create it, following the same guidelines as the existing plugins:

On your source packages [Java], add the following classes:

Package: com.gluonhq.charm.down.plugins. Class: SMSService:

package com.gluonhq.charm.down.plugins;

public interface SMSService {
    void sendSMS(String number);
}

Package: com.gluonhq.charm.down.plugins. Class: SMSServiceFactory:

package com.gluonhq.charm.down.plugins;

import com.gluonhq.charm.down.DefaultServiceFactory;

public class SMSServiceFactory extends DefaultServiceFactory<SMSService> {

    public SMSServiceFactory() {
        super(SMSService.class);
    }

}

Finally, on the Android package, implement the service:

Package: com.gluonhq.charm.down.plugins.android, class: AndroidSMSService

package com.gluonhq.charm.down.plugins.android;

import android.content.Intent;
import android.net.Uri;
import com.gluonhq.charm.down.plugins.SMSService;
import javafxports.android.FXActivity;

public class AndroidSMSService implements SMSService {

    @Override
    public void sendSMS(String number) {
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);         
        smsIntent.setData(Uri.parse("sms:"));
        smsIntent.putExtra("address", number);
        FXActivity.getInstance().startActivity(smsIntent);
    }

}

Test

Now all you need to do is use the SMS service in your code, once you have the number added to the textField, and a button to fire the action:

Services.get(SMSService.class)
            .ifPresent(s -> button.setOnAction(e -> s.sendSMS(textField.getText())));

Build the application and deploy it to your Android device. Try it, and check that it opens the Messenger app (maybe it will ask to use other different apps), and already uses the provided number, so you can start typing your message.