Siva K
Siva K

Reputation: 4968

android show an activity for every 24 hrs

in my app next to the splash screen i need to show an activity called Tips Page. This activity to be shown once in a day. When the user opens the app again within 24 hrs it should not be shown.

First i tried to show based on the current date, when the first the activity shown i will store the current date in shared preference and next time when the app gets opened i will check whether the current date and date in shared preference are equal or not. If equal i will not show the activity if not i will show the activity.

But here there is a logic mistake, if the user opens the app first in midnight of 11 PM in a date, and again opens the app after t hour the Tips activity will be shown, but i need to show it after 24 hrs, how can it be done. pls help me in this ligic

Upvotes: 1

Views: 398

Answers (3)

Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12695

I think you need such a flow, see this the implementation of Jason Hessley's Answer

 SharedPreferences settings = getSharedPreferences("Preferences",
            MODE_PRIVATE);

    long timeFromPrefs = settings.getLong("time", System.currentTimeMillis());
    final long TIME_DIFF = 24*60*60*1000; 


    if ((System.currentTimeMillis()-timeFromPrefs)>TIME_DIFF) {
        // show Activity...........
        Editor editor = settings.edit();
        editor.putLong("time", System.currentTimeMillis());
        editor.commit();

    } 

Upvotes: 1

san
san

Reputation: 1835

Android applications can run periodic timers using android.os.Handler & java.lang.Runnable classes. As simple example is shown below.

Key Points 1. Service classes extending android.app.Service should implement onBind, onCreate & onDestroy, life cycle methods. 2. periodicTask is an instance of Runnable implementation, that runs a Thread. Execution of run() will print the message "Awake". 3. mHandler is an instance of Handler, that is attached the periodicTask thread. 4. The Handler is informed to execute the thread every minute, by postDelayed. 5. When the service is destroyed, the periodicTask instance is removed from the Handler, by invoking removeCallbacks.

Sample Code

public class PeriodicTimerService extends Service {
    private Handler mHandler = new Handler();
    public static final int ONE_DAY = 86400000;
    private Runnable periodicTask = new Runnable() {
        public void run() {
            Log.v("PeriodicTimerService","Awake");
            mHandler.postDelayed(periodicTask, ONE_DAY );
        }
    };

   @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        mHandler.postDelayed(periodicTask, ONE_DAY );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(periodicTask);
        Toast.makeText(this, "Service onDestroy() ", Toast.LENGTH_LONG).show();
    }
}

Upvotes: 1

Jason Hessley
Jason Hessley

Reputation: 1628

Save the last time the activity was started in a shared pref as you stated in your question. Then subtract the last time from the current time. If it is greater then 24 hours, show your tips.

Upvotes: 0

Related Questions