Kareem Essam Gaber
Kareem Essam Gaber

Reputation: 643

Keep service running in background without showing notification

I'm developing a messaging application and I need my service to keep running in background even if the application was closed but without showing a notification in navigation bar and start when the phone is started and restart itself when it is closed for any reason, I know this question has been asked before but I can find nothing to do this, I just want it to be like whatsapp or facebook or bbm services, thanks in advance.

Upvotes: 2

Views: 2906

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

I need my service to keep running in background even if the application was closed but without showing a notification in navigation bar and start when the phone is started and restart itself when it is closed for any reason

That is not strictly possible.

You are welcome to return START_STICKY or START_REDELIVER_INTENT from your service's onStartCommand() method. Android will still terminate your process due to old age and low memory conditions from time to time, but Android will eventually restart those services. What percentage of the time your process will be running will depend on a variety of factors, not the least of which being how much system RAM the Android device has.

Better yet, you are welcome to use Google Cloud Messaging (GCM) to deliver your messages to your app. This way, you do not need a service running all of the time. Your app can get control when a message arrives, do some work for that message, then go away.

I just want it to be like whatsapp or facebook or bbm services

BlackBerry Messenger uses startForeground() and has an icon in the status bar as a result.

Upvotes: 3

Related Questions