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:
- Create your usual normal service, and make the service foreground.
- Create another service, make this service too foreground, using the same code as above, with the same notification id.
- Now all you have to do is, after making the second service foreground, you need to stop it running from foreground.
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
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.
Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
ReplyDeleteAndroid Training in chennai |Android Training in Velachery
click here for info.
ReplyDeleteThe article is so informative. This is more useful for our
ReplyDeleteAngular JS Training in Chennai | Certification | Online Training Course | Angular JS Training in Bangalore | Certification | Online Training Course | Angular JS Training in Hyderabad | Certification | Online Training Course | Angular JS Training in Coimbatore | Certification | Online Training Course | Angular JS Training | Certification | Angular JS Online Training Course
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeletesap training in chennai
sap training in omr
azure training in chennai
azure training in omr
cyber security course in chennai
cyber security course in omr
ethical hacking course in chennai
ethical hacking course in omr
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
Tiktok Jeton Hilesi
SAC EKİMİ ANTALYA
INSTAGRAM TAKİPCİ
instagram takipçi satın al
metin2 pvp serverlar
Takipçi satın al
En Son Çıkan Perde Modelleri
ReplyDeletesms onay
mobil ödeme bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
Trafik sigortasi
Dedektor
web sitesi kurma
ask romanlari