【发布时间】:2020-01-18 17:01:31
【问题描述】:
我正在制作一个连接到亚马逊 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());
}
}
};
我希望能够完全删除“连接”按钮,或者至少将其作为“重新连接”按钮存在。 这样,当应用启动时,它就已经在连接,而不是等待用户输入。
【问题讨论】:
标签: java android android-studio button