title: Timer in the Service
body: <p>I have an app that records the time of the job, i mean that calculates the job time for that purpose i used chronometer to run the time. Since the job is 9 hours long so user can't open the app for that long. For that purpose i am using the Service which indicate the time. Since i cannot use Chronometer because</p>
  
  <p>Chronometer is a UI widget (actually a TextView) in Android. So, i can't use it for non-UI purposes. So i have to use timer to do the job. But i do not know how can i achieve this. Any code or help is appreciated. Thanks </p>
  
  <p>When i hit the start button the chronometer starts like <a href="https://i.stack.imgur.com/1SGOt.png" rel="nofollow noreferrer">this</a> 
  and my service also started but i get the time as 00:00:00 like <a href="https://i.stack.imgur.com/Tu08L.png" rel="nofollow noreferrer">this</a></p>
  
  <p>i just want that timer should run in the service and i can see the time in service and i do not want to deal or use the time that is running in service, the time in service will show user that how much time they spend on job. </p>
  
  <p><strong>Service Class</strong></p>
  
  <pre><code>public class ServiceTimer extends Service {
  
     // Chronometer chronometer ;
      //String valueOfTime ;
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          String input = intent.getStringExtra("inputExtra");
  
          // chronometer = new Chronometer(this);
          //chronometer.setText("00:00:00");
          //chronometer.setOnChronometerTickListener(new //Chronometer.OnChronometerTickListener() {
  
    //              @Override
  //            public void onChronometerTick(Chronometer chronometer) {
    //              CharSequence text = chronometer.getText();
      //            if (text.length()  == 5) {
        //              chronometer.setText("00:"+text);
          //        } else if (text.length() == 7) {
            //          chronometer.setText("0"+text);
              //    }
              //}
     //         });
  
  
  
  
  
   //       chronometer.start();
      //before i know about chronometer that i cannot use it in service 
  // this is what i have so for with chronometer to achieve 
  // but failed ...
          Intent notificationIntent = new Intent(this, Timer_FullTime.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(this,
                  0, notificationIntent, 0);
  
  
  
          Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                  .setContentTitle(chronometer.getText().toString())
                  .setSmallIcon(R.mipmap.logoback)
                  .setContentText(input)
                  .setContentIntent(pendingIntent)
                  .build();
  
          startForeground(1, notification);
  
          //do heavy work on a background thread
          //stopSelf();
  
          return START_NOT_STICKY;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>  <strong>this is how i get what i wanted or asked in the question</strong> </p>
  
  <pre><code>public class ServiceTimer extends Service {
  
      private   int THE_ID_TO_UPDATE = 1;
      private static Timer timer = new Timer();
      private Context ctx;
      private int second = 0 ;
      NotificationManager notificationManager ;
      private int minute = 0 ;
      private int hour = 0 ;
  
      //we are going to use a handler to be able to run in our TimerTask
      final Handler handler = new Handler();
      NotificationCompat.Builder notification ;
      @Override
      public void onCreate() {
          super.onCreate();
          super.onCreate();
          ctx = this;
      }
  
  
      private class mainTask extends TimerTask
      {
          public void run()
          {
  
              second = second + 1 ;
  
              if (second == 60){
                  minute++ ;
                  second = 0 ;
              }
  
              if (minute == 60){
                  hour++;
                  minute = 0 ;
                  second = 0 ;
  
              }
  
  
              notification.setContentText( hour + "h " + minute + "m " + second+"s");
  
  
  
              notificationManager.notify(THE_ID_TO_UPDATE , notification.build());
  
  
          }
      }
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
  
  
          String input = intent.getStringExtra("inputExtra");
  
          Intent notificationIntent = new Intent(this, Timer_FullTime.class) ;
          PendingIntent pendingIntent = PendingIntent.getActivity(this,
                  0, notificationIntent, 0) ;
  
  
  
          notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          notification = new NotificationCompat.Builder(this, CHANNEL_ID);
  
  
          notification.setContentTitle(input);
          notification.setSmallIcon(R.mipmap.logoback);
          notification.setOnlyAlertOnce(true);
          notification.setWhen(System.currentTimeMillis());
  
          notification.setContentIntent(pendingIntent);
          notification.setLights(Color.RED, 1000, 1000);
          notification.setVibrate(new long[]{0, 400, 250, 400});
          notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
  
          notification.build();
          notificationManager.notify(THE_ID_TO_UPDATE , notification.build());
          startForeground(THE_ID_TO_UPDATE, notification.build());
  
          timer.scheduleAtFixedRate(new mainTask(), 0, 1000);
  
  
  
          return START_NOT_STICKY;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          timer.cancel();
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  }
  </code></pre>