java - 如何在按下按钮之前激活按钮?

标签 java android android-studio button

我正在制作一个连接到亚马逊AWS服务的应用程序。我的一切都已正确连接,但在连接之前我需要按一个按钮。有没有办法避免这一步并让它自动连接到AWS?

现在,用户必须按下一个按钮表示他们想要连接,然后按下另一个按钮表示他们想要订阅主题以接收更新。由于此应用程序的唯一目的是连接到 AWS,因此我想删除按钮按下操作,因为这只是浪费时间。

这是我设置连接的教程,以防它提供更好的信息:https://www.linkedin.com/pulse/android-app-aws-iot-core-guide-felipe-ramos-da-silva

否则,这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context = PubSubActivity.this;

        //Sets up layout information
        txtSubscribe = (EditText) findViewById(R.id.txtSubscribe);
        tvClientId = (TextView) findViewById(R.id.tvClientId);
        tvStatus = (TextView) findViewById(R.id.tvStatus);

        tvSteamTemp = (TextView) findViewById(R.id.tvSteamTemp);
        tvWaterTemp = (TextView) findViewById(R.id.tvWaterTemp);
        tvWaterFlow = (TextView) findViewById(R.id.tvWaterFlow);
        tvDieselFlow = (TextView) findViewById(R.id.tvDieselFlow);
        tvManualResetLevel = (TextView) findViewById(R.id.tvManualResetLevel);
        tvWaterFeederLevel = (TextView) findViewById(R.id.tvWaterFeederLevel);
        tvAutoResetPressure = (TextView) findViewById(R.id.tvAutoResetPressure);
        tvManualResetPressure = (TextView) findViewById(R.id.tvManualResetPressure);
        tvTempLimit = (TextView) findViewById(R.id.tvTempLimit);

        btnConnect = (Button) findViewById(R.id.btnConnect);
        btnConnect.setOnClickListener(connectClick);
        btnConnect.setEnabled(false);

        btnSubscribe = (Button) findViewById(R.id.btnSubscribe);
        btnSubscribe.setOnClickListener(subscribeClick);

        btnDisconnect = (Button) findViewById(R.id.btnDisconnect);
        btnDisconnect.setOnClickListener(disconnectClick);


        /* MQTT client IDs are required to be unique per AWS IoT account.
         * This UUID is "practically unique" but does not _guarantee_
         * uniqueness.
         */

        clientId = UUID.randomUUID().toString();
        tvClientId.setText(clientId);

        // Initialize the AWS Cognito credentials provider
        // Sends info to AWS so it knows to what it needs to connect
        credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(), // context
                COGNITO_POOL_ID, // Identity Pool ID
                MY_REGION // Region
        );

        // MQTT Client
        /* Sets up the app side of being able to read and understand
         * information sent from AWS
         */
        mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);

        // The following block uses a Cognito credentials provider for authentication with AWS IoT.
        new Thread(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnConnect.setEnabled(true);
                    }
                });
            }
        }).start();
    }

这就是单击连接按钮时发生的情况:

    View.OnClickListener connectClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(LOG_TAG, "clientId = " + clientId);

            try {
                mqttManager.connect(credentialsProvider, new AWSIotMqttClientStatusCallback() {
                    @Override
                    public void onStatusChanged(final AWSIotMqttClientStatus status, final Throwable throwable) {
                        Log.d(LOG_TAG, "Status = " + status);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (status == AWSIotMqttClientStatus.Connecting) {
                                    tvStatus.setText("Connecting...");

                                } else if (status == AWSIotMqttClientStatus.Connected) {
                                    tvStatus.setText("Connected");

                                } else if (status == AWSIotMqttClientStatus.Reconnecting) {
                                    if (throwable != null) {
                                        Log.e(LOG_TAG, "Connection error.", throwable);
                                    }
                                    tvStatus.setText("Reconnecting");
                                } else if (status == AWSIotMqttClientStatus.ConnectionLost) {
                                    if (throwable != null) {
                                        Log.e(LOG_TAG, "Connection error.", throwable);
                                        throwable.printStackTrace();
                                    }
                                    tvStatus.setText("Disconnected");
                                } else {
                                    tvStatus.setText("Disconnected");

                                }
                            }
                        });
                    }
                });
            } catch (final Exception e) {
                Log.e(LOG_TAG, "Connection error.", e);
                tvStatus.setText("Error! " + e.getMessage());
            }
        }
    };

我希望能够完全删除“连接”按钮,或者至少将其作为“重新连接”按钮存在。 这样,当应用程序启动时,它就已经在连接,而不是等待用户输入。

最佳答案

btnConnect.setEnabled(true);之后添加调用btnConnect.performClick()

我不知道为什么你必须在 acitivity onCreate 方法中创建新线程,然后使用 runOnUiHandle 在 UI 线程上运行它。 onCreate方法默认运行在UI线程上

关于java - 如何在按下按钮之前激活按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980879/

相关文章:

java - 如何将所有不同类型的定位器(xpath、Css、Link、ID 等)存储在 selenium Web 驱动程序的单个变量中

java - 如何从 XML 配置实例化 fragment ?

java - 如何将相机照片保存到移动设备的文件中?

jenkins - 让 gradle 执行 JUnit 测试(Android 应用程序,Android Studio)

java - Android Studio 无法正确显示 xml 文件

android - 如何找到官方的android studio旧版本

java - Spring Cloud Config Server 与云 kubernetes 的 ConfigMaps

java - 解析 - 为什么 C++ 在模板 var decls 上有问题而 java 在通用 var decls 上却没有?

java - Primefaces 数据表中的样式 ENUM

android - 如何以编程方式设置 LinearLayout 的重力和布局重力