0

I want the functionality of Google Now app, like when it's day time, the header is a sunny image, when it becomes noon or sundown, the image changes to a sundown image, when it's night the image changes to a night image, and same for the morning one.

I'm trying to implement my background which does this very same thing, how must I go about implementing this? I've searched up on this but the answers are for html and website development.

And most of the others are based on time interval and I think that's what I should use but I would like something like this. Written in non-tech language

01:00/1am - Morning - Image changes to Morning.png on the imageview (R.id.view).

09:00/9am - Normal - Image changes to Daytime.png on the imageview (R.id.view).

12:00/12pm - Noon - Image changes to Noon.png on the imageview (R.id.view).

19:00/7pm - Night - Image changes to Noon.png on the imageview (R.id.view).

How I can achieve something similar to this?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Robin
  • 577
  • 1
  • 7
  • 13
  • do You want it only for an app or a widget? app should be easy: get the current time everytime the app is opened and change the Image programmatically. One possible way is to do a layout with an imageView as Header. Then get the time in Your app and refer to this imageView, and then just change the pic You want. And if it should work across time, you can do this with an Alarm Manager. – Opiatefuchs May 21 '14 at 06:59
  • yeah I figured that would be a good idea, and for my application. – Robin May 21 '14 at 07:04

2 Answers2

0

I advice you write a class which has a listen method. This listen method which checks time and raises a custom event (you can use interface here) on Activity level must be called periodically. You can use Timer and TimerTask or CountdownTimer to call.

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
0

Well the best candidate for this job is an AlarmManager!

Lets assume you only need to change the background when your Activity is running. You can set up your alarm when you Create your Activity:

private PendingIntent pi=null;
private AlarmManager mgr=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mgr=(AlarmManager)getSystemService(ALARM_SERVICE);
    pi=createPendingResult(ALARM_ID, new Intent(), 0);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
    SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
}

PERIOD (ms) is how often we want to get control (onActivityResult). The createPendingResult(ALARM_ID, new Intent(), 0); line creates an Intent that can be caught in your Activities onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == ALARM_ID) {
      // This is where you check the time and change your background!
   }
}

You also need to cancel your alarm in onDestroy:

@Override
public void onDestroy() {
    mgr.cancel(pi);
    super.onDestroy();
}

To check if a Date is in a specific interval you can use:

boolean isWithinRange(Date testDate) {
    return testDate.getTime() >= startDate.getTime() &&
             testDate.getTime() <= endDate.getTime();
}
VM4
  • 6,321
  • 5
  • 37
  • 51
  • Thanks for the explanation and code sample, in the second method, what am I goingto have to use to check the time of day and then display the image, DateUtils? – Robin May 21 '14 at 07:10
  • I've updated my answer, you can use a simple Date and use the method I got from http://stackoverflow.com/a/494203/1803821 to check if it's in your required interval. – VM4 May 21 '14 at 07:14