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

China☆狼群 提交于 2019-12-02 12:38:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!