final Handler handler = new Handler(); final Timer timer = new Timer(); final String[] lastPeriod = {""}; final boolean[] firstPredictionDone = {false}; final MediaPlayer[] sound = {null}; timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { Calendar calendar = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC")); int seconds = calendar.get(Calendar.SECOND); int remainingSeconds = 60 - seconds; int minutes = calendar.get(Calendar.MINUTE); int totalMinutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + minutes; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd", java.util.Locale.getDefault()); String datePart = sdf.format(calendar.getTime()); long periodSuffix = 10001 + totalMinutes; String fullPeriod = datePart + "1000" + periodSuffix; // Update period & seconds periodno.setText(fullPeriod); secondno.setText(String.valueOf(remainingSeconds)); // 1st prediction on app open if (!firstPredictionDone[0]) { firstPredictionDone[0] = true; lastPeriod[0] = fullPeriod; int result = new java.util.Random().nextInt(10); String prediction = (result >= 5) ? "Big" : "Small"; textview3.setText(prediction); android.graphics.drawable.GradientDrawable shape = new android.graphics.drawable.GradientDrawable(); shape.setCornerRadius(59f); if (result >= 5) { shape.setColor(android.graphics.Color.parseColor("#FFCC00")); // Big } else { shape.setColor(android.graphics.Color.parseColor("#2196F3")); // Small } bigsmalllinear.setBackground(shape); // Play sound sound[0] = MediaPlayer.create(MainActivity.this, R.raw.change); if (sound[0] != null) sound[0].start(); return; } // If period changed รข†’ update result if (!lastPeriod[0].equals(fullPeriod)) { lastPeriod[0] = fullPeriod; int result = new java.util.Random().nextInt(10); String prediction = (result >= 5) ? "Big" : "Small"; textview3.setText(prediction); android.graphics.drawable.GradientDrawable shape = new android.graphics.drawable.GradientDrawable(); shape.setCornerRadius(59f); if (result >= 5) { shape.setColor(android.graphics.Color.parseColor("#FFCC00")); // Big } else { shape.setColor(android.graphics.Color.parseColor("#2196F3")); // Small } bigsmalllinear.setBackground(shape); // Play sound if (sound[0] != null) sound[0].release(); sound[0] = MediaPlayer.create(MainActivity.this, R.raw.change); if (sound[0] != null) sound[0].start(); } } }); } }, 0, 1000);

Post a Comment