title: Android Listener stop running when app in background
body: <p>I am developing an app where the app will detect Bluetooth signals (Sensoro Smart Beacon device) and open the activity. But I want the app to still be able to detect the signal even when the application on the background or even when killed. I used a foreground service, it detects the signal when I open the application and move between activities but when sending the app to the background and opening other applications, the listener stops although the service still working. I am printing the logs. <code>System.out.println(&quot;Sensoro 2&quot; );</code> keeps printing even when I kill the application or open another application. But the printing logs in BeaconManagerListener are not working. I tried to use background service but it didn't work also.
  Can you please advise if there is a way to make the listener works in a service when the app in background or killed?
  Here is the service code:</p>
  <pre><code>public class MyService extends Service {
      public static final String CHANNEL_ID = &quot;ForegroundServiceChannel&quot;;
      int service_timer=0;
      Timer timer = new Timer();
      SensoroManager sensoroManager;
      @Override
      public void onCreate() {
          super.onCreate();
      }
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          sensoroManager = SensoroManager.getInstance(MyService.this);
          String input = intent.getStringExtra(&quot;inputExtra&quot;);
          System.out.println(&quot;Sensoro 2&quot; );
          createNotificationChannel();
          Intent notificationIntent = new Intent(this, MainActivity.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(this,
                  0, notificationIntent, 0);
          Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                  .setContentTitle(&quot;Foreground Service&quot;)
                  .setContentText(input)
                  .setSmallIcon(R.drawable.ic_launcher_background)
                  .setContentIntent(pendingIntent)
                  .build();
          startForeground(1,notification);
          //do heavy work on a background thread
          //stopSelf();
                  if (sensoroManager.isBluetoothEnabled()) {
                      sensoroManager.setCloudServiceEnable(true);
                      /**
                       * Enable SDK service
                       **/
                      try {
                          sensoroManager.startService();
  
                      } catch (Exception e) {
                          e.printStackTrace(); // Fetch abnormal info
                      }
                  }
                  else
                  {
                      Toast.makeText(MyService.this,&quot;Bluetooth off&quot;,Toast.LENGTH_LONG).show();
                  }
          new Timer().scheduleAtFixedRate(new TimerTask() {
              @Override
             public void run() {
                  //your method
                  System.out.println(&quot;Sensoro 2&quot; );
  
  
                  BeaconManagerListener beaconManagerListener = new BeaconManagerListener() {
  
                      @Override
                      public void onUpdateBeacon(ArrayList&lt;Beacon&gt; beacons) {
                          // Refresh sensor info
                          for (Beacon beacon : beacons
                          ) {
                              System.out.println(&quot;Sensoro 3&quot; );
                             // System.out.println(&quot;Sensoro&quot; +beacon.getAccuracy());
                          }
                      }
  
                      @Override
                      public void onNewBeacon(Beacon beacon) {
  
                          if (beacon.getSerialNumber().equals(&quot;0117C59B243C&quot;)){
                              System.out.println(&quot;Sensoro 3&quot; );
                              System.out.println(&quot;Sensoro acc&quot; +beacon.getAccuracy());
                  }
              }
  
                      @Override
                      public void onGoneBeacon(Beacon beacon) {
  
                          if (beacon.getSerialNumber().equals(&quot;0117C59B243C&quot;)){
                              System.out.println(&quot;Sensoro acc gone&quot;);
                              System.out.println(&quot;Sensoro acc Timer&quot; +service_timer);
                          }
                      }
                  };
                  sensoroManager.setBeaconManagerListener(beaconManagerListener);
              }
          }, 0, 2000);
  
  
  
          return START_NOT_STICKY;
      }
      @Override
      public void onDestroy() {
          super.onDestroy();
         // timer.cancel();
      }
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
      private void createNotificationChannel() {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              NotificationChannel serviceChannel = new NotificationChannel(
                      CHANNEL_ID,
                      &quot;Foreground Service Channel&quot;,
                      NotificationManager.IMPORTANCE_DEFAULT
              );
              NotificationManager manager = getSystemService(NotificationManager.class);
              manager.createNotificationChannel(serviceChannel);
          }
      }
  
  
  }
  </code></pre>
  <p>Here is where I call it:</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          startService();
          }
  
  
      public void startService() {
          Intent serviceIntent = new Intent(this, MyService.class);
          serviceIntent.putExtra(&quot;inputExtra&quot;, &quot;Foreground Service Example in Android&quot;);
          System.out.println(&quot;Sensoro 1 &quot;);
          ContextCompat.startForegroundService(this, serviceIntent);
      }
      public void stopService() {
          Intent serviceIntent = new Intent(this, MyService2.class);
          stopService(serviceIntent);
      }
  
  
      public void movingg(View view) {
          Intent intent=new Intent(this,Usermain.class);
         startActivity(intent);    }
  }
  </code></pre>