0

So I found this background service code that keeps on popping up a toasts every 10 seconds. What I want to ask is translate this code on popping up in minutes interval instead of 10 seconds of interval and instead of a toast popping up I want to it to popup an activity when the interval finishes.

public class TimeService extends Service {
    // constant
    public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds

    // run on another Thread to avoid crash
    private Handler mHandler = new Handler();
    // timer handling
    private Timer mTimer = null;

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

    @Override
    public void onCreate() {
        // cancel if already existed
        if(mTimer != null) {
            mTimer.cancel();
        } else {
            // recreate new
            mTimer = new Timer();
        }
        // schedule task
        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
    }

    class TimeDisplayTimerTask extends TimerTask {

        @Override
        public void run() {
            // run on another thread
            mHandler.post(new Runnable() {

                @Override
                public void run() {

                    // display toast
                    Toast.makeText(getApplicationContext(), getDateTime(),
                            Toast.LENGTH_SHORT).show();
                }

            });
        }

        private String getDateTime() {
            // get date time in custom format
            SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
            return sdf.format(new Date());
        }


    }
 ...
}
Skynet
  • 7,820
  • 5
  • 44
  • 80
Jade Jose
  • 11
  • 2
  • pop up Activity? i guess you need `popupwindow` or `alertdialog` – Manohar Feb 24 '17 at 04:44
  • Possible duplicate of [android start activity from service](http://stackoverflow.com/questions/3606596/android-start-activity-from-service) – Skynet Feb 24 '17 at 04:44

2 Answers2

0
public static final long NOTIFY_INTERVAL = 10 * 1000 * 60;
divyansh ingle
  • 340
  • 1
  • 9
0

10*1000 is 10 seconds.Time is always counted as milliseconds. so change 10 * 1000 to 10 * 1000 * 60

public static final long NOTIFY_INTERVAL = 10 * 1000 * 60;
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57