I'm working on an android studio project trying to get an image to change every half an hour, I followed an example but am having no luck; it throws no errors, it just doesen't work, here's what I have and what I've tried:
public class MainActivity extends AppCompatActivity {
long startTime = 0;
Handler feed = new Handler();
Runnable runTimer = new Runnable()
{
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
output.setText(String.format("%d:%02d", minutes, seconds));
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
feed.postDelayed(this, 500); // tried moving this around and changing the value from 0 - 3000
}
startTime = System.currentTimeMillis();
feed.postDelayed(runTimer, 500); // tried moving this around and changing the value from 0 - 3000
}
};
To test my code I put this in an onUserInteraction method and it works perfectly fine:
@Override
public void onUserInteraction() {
super.onUserInteraction();
final Globals g = (Globals) getApplication();
Calendar now = Calendar.getInstance();
ImageView roto = (ImageView) findViewById(R.id.earthView);
int vision = 0;
int visionb = 0;
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {
visionb = 1;
}
if (minute < 30) {
visionb = 0;
}
vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
So I assume I'm doing something wrong, Anyone know what I'm doing wrong or missing?
Thanks for the help Landice This is the working result, giving me both a clock and every 1/2 hour of the day changes my image (which has 48 total images), again thanks for your help!
long startTime = 0;
{
final Handler feed = new Handler();
final Runnable runTimer = new Runnable() {
@Override
public void run() {
Calendar now = Calendar.getInstance();
TextView output = (TextView) findViewById(R.id.output);
ImageView roto = (ImageView) findViewById(R.id.earthView);
Globals g = (Globals) getApplication();
int vision = 0;
int visionb = 0;
int second = now.get(Calendar.SECOND);
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
String[] epos = g.getData56();
if (minute >= 30) {visionb = 1;}
if (minute < 30) {visionb = 0;}
output.setText(String.format(hour + ":" + minute + ":" + second));
feed.postDelayed(this, 0);
if (minute == 30 || minute == 0) {vision = ((hour * 2) + visionb);
int resID = getResources().getIdentifier(epos[vision], "drawable", getPackageName());
if (roto != null) {
roto.setImageResource(resID);
}
}
startTime = System.currentTimeMillis();
}
};
feed.postDelayed(runTimer,1000);
}