title: Service freezing the UI
body: <p>I have a service which counts steps from the sensor data. It's a continuously running service. My service is running and also doing the desired task but my main problem is its freezing my UI. How can I update the UI without freezing it.</p>
  
  <p>Here is my service class:</p>
  
  <pre><code>public class StepCounterService extends Service {
      private static final String LOG_TAG = "ForegroundService";
  
      public static Boolean FLAG = false;
  
      private SensorManager mSensorManager;
      private StepDetector detector;
  
      private PowerManager mPowerManager;
      private WakeLock mWakeLock;
  
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          startServiceForeground(intent, flags, startId);
  
  
          return START_STICKY;
      }
  
      @Override
      public void onCreate() {
          // TODO Auto-generated method stub
          super.onCreate();
          new StepCountManager(this);
  
  
          FLAG = true;
  
          Log.e("Service_Started", "");
          detector = new StepDetector(this);
  
  
          mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  
          mSensorManager.registerListener(detector,
                  mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                  SensorManager.SENSOR_DELAY_NORMAL);
  
  
          mPowerManager = (PowerManager) this
                  .getSystemService(Context.POWER_SERVICE);
          mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK
                  | PowerManager.ACQUIRE_CAUSES_WAKEUP, "S");
          mWakeLock.acquire();
          reloadSettings();
      }
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
          super.onDestroy();
          FLAG = false;
          if (detector != null) {
              mSensorManager.unregisterListener(detector);
          }
  
          if (mWakeLock != null) {
              mWakeLock.release();
          }
  
          Log.e("Service_destroyed", "");
      }
  
      public void reloadSettings() {
  
          if (detector != null) {
              detector.setSensitivity(
                      Float.valueOf("10")
              );
          }
  
  
      }
  
      @Override
      public void onTaskRemoved(Intent rootIntent) {
          super.onTaskRemoved(rootIntent);
          Intent restartService = new Intent(getApplicationContext(),
                  this.getClass());
          restartService.setPackage(getPackageName());
          PendingIntent restartServicePI = PendingIntent.getService(
                  getApplicationContext(), 1, restartService,
                  PendingIntent.FLAG_ONE_SHOT);
  
          //Restart the service once it has been killed android
  
          ((AlarmManager) getSystemService(Context.ALARM_SERVICE))
                  .set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent
                          .getService(this, 3, new Intent(this, StepCounterService.class), 0));
  
      }
  
      public int startServiceForeground(Intent intent, int flags, int startId) {
          Intent notificationIntent = new Intent(this, HomeActivity.class);
          notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          Notification notification = new NotificationCompat.Builder(this)
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle("Mobiefit Walk")
                  .setContentIntent(pendingIntent)
                  .setOngoing(true)
                  .build();
          NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          mNotificationManager.notify(300, notification);
          Notification n;
  
          startForeground(300, notification);
  
          return START_STICKY;
      }
  
  
  }
  </code></pre>
  
  <p>And here is my UpdateStepCount method which is declare in an Interface class and I am implementing it in seperate class:</p>
  
  <pre><code>public void UpdateStepCount(final int steps) {
          if (Target_Goal == 0) {
              Target_Goal = 10000;
          }
          resetData(steps);
  
          Log.i("Narendra", "Total Steps:::" + String.valueOf(total_steps) + "::Steps::" + steps + "::getlast count:" + MobieFitWalkApp.getInstance().getLastStepCount());
          total_steps = steps - MobieFitWalkApp.getInstance().getLastStepCount();
          Log.i("getDistance",String.valueOf(Utilities.convertKilometersToMeters(getDistance(total_steps))));
          Log.i("CountCalories",String.valueOf(countCalories(total_steps)));
         // sessionDetails = SummaryManager.singleton().getLastFreeWalkData((Activity) mContext);
  
         // getFreeWalkData(total_steps);
  
          updateNotification(total_steps);
          if (needToStore) {
  
              if (dbController != null) {
                  if (dbController.isTodayDataPresent(getCurrentDate())) {
                      //we need to get data from db
                      //calibrate previous and current data
                      //we need to write code for update data in db
  
                      dbController.updateSteps(String.valueOf(getDistance(total_steps)), String.valueOf(""), String.valueOf(total_steps), String.valueOf((Utilities.round(countCalories(total_steps), 2))));
                      needToStore = false;
                  } else {
                      //insert new row
                      storeStepsDataInDb(total_steps);
                      needToStore = false;
  
                  }
              }
              //call back will take total_steps,kcal,distance
              if (updateUI != null) {
                  updateUI.UpdateBackgroundStepsUI(total_steps, getDistance(total_steps), countCalories(total_steps));
              }
  
  
              new Handler().postDelayed(new Runnable() {
                  public void run() {
                      // Log.i("xxx", "Sensor changed");
                      needToStore = true;
  
                  }
              }, 1000);
          }
  
      }
  </code></pre>
  
  <p>Can Someone tell me how can I stop my UI from getting freeze.</p>