如何在 JAVAFX/Gluon 中实现一个按钮以转到我的 Android 移动设备的默认发送消息

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

我在我的移动应用程序中实现了一个按钮,并且有一个带有电话号码的文本字段。

我想要实现的目标:当用户单击按钮时-> 发送 SMS 的默认页面在 Android 中打开并且 Telefonnummer 粘贴在 Android 的默认数字文本字段中。

我将 Gluon Mobile 与 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.

我建议你看看 Charm Down 插件是怎样的implemented

您会发现无需进一步修改即可使用大部分现有服务。

如果您正在寻找的服务尚未实现,创建它真的很容易,遵循与现有插件相同的指南:

在您的源包 [Java] 上,添加以下 classes:

包裹:com.gluonhq.charm.down.plugins。 Class: SMSService:

package com.gluonhq.charm.down.plugins;

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

包裹: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);
    }

}

最后,在Android包上,实现服务:

包裹: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);
    }

}

测试

现在您需要做的就是在您的代码中使用 SMS 服务,一旦您将号码添加到 textField,以及一个触发操作的按钮:

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

构建应用程序并将其部署到您的 Android 设备。尝试一下,检查它是否打开了 Messenger 应用程序(也许它会要求使用其他不同的应用程序),并且已经使用了提供的号码,因此您可以开始输入消息。