我正在制作一个连接到Amazon的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());
}
}
};
我希望能够完全删除“连接”按钮,或者至少将其作为“重新连接”按钮。 这样,当应用启动时,它已经在连接而不是等待用户输入。
答案 0 :(得分:1)
在btnConnect.performClick()
之后添加通话btnConnect.setEnabled(true);
我不知道为什么您必须在活动onCreate方法中创建新的线程,然后使用runOnUiHandle在UI线程上运行它。默认情况下,onCreate方法在UI线程上运行
答案 1 :(得分:0)
创建一个包含connectClick
内容的函数,并在onCreate
中调用它。由于您没有在v
中使用参数connectClick
,因此该函数不需要任何参数。