public class MainActivity extends AppCompatActivity {
DatabaseReference databaseHistory;
HistoryTremor historyTremor;
private int seconds = 0;
private boolean startRun;
TextView txtStatus2, txtTanggal, txtDurasi;
ListView lsvHistory1;
Button btnHistory;
String[] ListElements = new String[]{};
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
TextClock textClock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
txtStatus2 = (TextView) findViewById(R.id.txtStatus2);
txtTanggal = (TextView) findViewById(R.id.txtTanggal);
txtDurasi = (TextView) findViewById(R.id.txtDurasi);
lsvHistory1 = (ListView) findViewById(R.id.lsvHistory1);
textClock = (TextClock) findViewById(R.id.textClock);
btnHistory = (Button) findViewById(R.id.btnHistory);
databaseHistory = FirebaseDatabase.getInstance().getReference("HistoryTremor");
startMqtt();
if (savedInstanceState != null) {
seconds = savedInstanceState.getInt("seconds");
startRun = savedInstanceState.getBoolean("startRun");
}
txtTanggal.setText(currentDateTimeString);
Timer();
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
ListElementsArrayList
);
lsvHistory1.setAdapter(adapter);
btnHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, TampilHistory.class);
startActivity(i);
}
});
}
private void startMqtt() {
final MqttHelper mqttHelper = new MqttHelper(getApplicationContext());
mqttHelper.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean b, String s) {
}
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
Log.w("Debug", mqttMessage.toString());
if (mqttMessage.toString().equals("1"))
{
startRun = true;
txtStatus2.setText("Sedang Terjadi Tremor");
}
if (mqttMessage.toString().equals("0"))
{
startRun = false;
saveHistory();
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
}
public void onSaveInstanceState(Bundle saveInstanceState) {
saveInstanceState.putInt("seconds", seconds);
saveInstanceState.putBoolean("startRun", startRun);
saveHistory();
}
private void Timer() {
final TextView txtDurasi = (TextView) findViewById(R.id.txtDurasi);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int minutes = seconds / 3600;
int second = (seconds % 3600) / 60;
int milis = (seconds % 60);
String time = String.format("%d:%02d:%02d", minutes, second, milis);
txtDurasi.setText(time);
if (startRun) {
seconds++;
}
handler.postDelayed(this, 0);
}
});
}
private void addHistory(){
String tanggal = txtTanggal.getText().toString();
String jam = textClock.getText().toString();
String durasi = txtDurasi.getText().toString();
String id = databaseHistory.push().getKey();
HistoryTremor historyTremor = new HistoryTremor(id, tanggal, jam, durasi);
databaseHistory.child(id).setValue(historyTremor);
}
private void saveHistory(){
startRun = false;
//seconds = 0;
ListElementsArrayList.add(txtTanggal.getText().toString() + " " + textClock.getText() + " " + txtDurasi.getText().toString());
Collections.reverse(ListElementsArrayList);
adapter.notifyDataSetChanged();
addHistory();
}
}
hay guys, I make some simple Internet of Things using android as my front end project I have some trouble in if statement (I think)
I do have 2 condition for my project
i need to save the timer data when the startrun is false but when i got 0 from my sensor it save the 0:00:00 data i didn't want to save the 0:00:00 data
so here's my code can u guys solve id
my if condition is onmessage methods
Aucun commentaire:
Enregistrer un commentaire