Making a service run in foreground without showing a notification

Services are very useful while trying to perform some long running operations, such as playing music, fetching data from the server, etc. But when the device's memory gets low, android tries to kill them services to free up some memory, and you don't want your active service to get killed. So to decrease the chance of getting killed, we can make the service run on foreground, which makes the device to think that, the service is performing some foreground service which the user is aware of. Foreground services need to show a sticky notification to remain in foreground, if the notification is dismissed then the service won't be in foreground anymore, but still there is way to trick android that the service is still in foreground without showing the notification.

Steps:
  1. Create your usual normal service, and make the service foreground.
  2.  Notification notification;  
               NotificationCompat.Builder bBuilder = new NotificationCompat.Builder(  
                         context).setSmallIcon(R.drawable.ic_noti)  
                         .setContentTitle("Timer")  
                         .setPriority(Notification.PRIORITY_MAX)  
                         .setContentText("Timer is running...").setOngoing(true);  
               notification = bBuilder.build();  
               notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;  
               notification.flags |= Notification.FLAG_NO_CLEAR;  
               notification.flags |= Notification.FLAG_ONGOING_EVENT;  
               startForeground(54312, notification);  //54312 is the notification id
    
  3. Create another service, make this service too foreground, using the same code as above, with the same notification id.
  4. Now all you have to do is, after making the second service foreground, you need to stop it running from foreground.
  5.  stopForeground(true);  
    

Here, using the same notification id in both services does the trick. All we did was made our service foreground, showed the notification, and again, we made another service foreground, and stopped it immediately, which pulls the notification down. Hence, the first service will be running in foreground without the notification.

Comments

  1. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai |Android Training in Velachery

    ReplyDelete
  2. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai | Best Android Training in chennai

    ReplyDelete
  3. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  4. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete

Post a Comment