Code example for Handler

Methods: hasMessagesremoveMessagessendEmptyMessage

0
 
        // Show notification when we start the timer 
        showNotification(); 
 
        mStart = System.currentTimeMillis();
        mHandler.removeMessages(0);
        mHandler.sendEmptyMessage(0);
         
        // Keep restarting until we stop the service 
        return START_STICKY;
    } 
 
    @Override 
    public void onDestroy() { 
        Log.i(TAG, "onDestroy");
        // Cancel the ongoing notification. 
        mNM.cancel(TIMER_NOTIFICATION);
         
        mHandler.removeMessages(0);
    } 
 
    @Override 
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: " + intent);
        return mBinder;
    } 
 
    public void stopTimer() { 
        mHandler.removeMessages(0);
        stopSelf();
        mNM.cancel(TIMER_NOTIFICATION);
         
        updateTask(); 
         
        // Broadcast timer stopped 
        Intent intent = new Intent(TimeTrackerActivity.ACTION_TIMER_STOPPED);
        intent.putExtra("time", mTime);
        sendBroadcast(intent);
    } 
 
    public boolean isTimerRunning() { 
        return mHandler.hasMessages(0);
    } 
 
    public void resetTimer() { 
        stopTimer(); 
        timerStopped(mTime);
Experience pair programming with AI  Get Codota for Java