gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:31:02 26 4
gpt4 key购买 nike

我正在制作一个连接到亚马逊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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com