Reputation: 105
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);
}
Upvotes: 1
Views: 146
Reputation: 56
You can try this answer: How to change images in imageview every n number of seconds.
int[] imageArray = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5 };
int position=0;
Timer mTimer = new Timer();
Upvotes: 0
Reputation: 81
I think you forgot to schedule the first post. This is an example of using Handler and Runnable to schedule periodical events. This line schedules the first run.
handler.post(runTimer);
And this line inside run() schedules for the afterward events at the time it runs:
handler.postDelayed(this, 1000);
Beware of that 1000 means one second. Therefore 30 minutes will be 60*1000*30 which is 1800000 in your case.
Example:
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textview);
final Handler handler = new Handler();
Runnable runTimer = new Runnable()
{
@Override
public void run() {
mTextView.setText("Current Time: " + (new Date()).toString());
handler.postDelayed(this, 1000);
}
};
handler.post(runTimer);
}
}
Also, you might want to stop the loop by removing the scheduled delayed event by:
handler.removeCallbacks(runTimer);
Upvotes: 2