I want to implement the startForeground() method in the Service class for prevent service self kill.
Can anybody sent me code for implementing this method?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Jovan, here is a method for making compatible code for 2.0+ and 1.6- (the code shows you how to detect which one is compatible)
http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
For 2.0+, I put together some example code (using startForeground). Observe that some code is now deprecated, however, Notification.Builder uses API level 11 (3.x) which means I won’t use that until most phones use a compatible Android version. Since the vast majority of phones is now running some version of 2.x, I think it’s safe enough to skip the compatibility check.
put this code within
onStartCommand()of your service and you’re good to go. (But you could put this section of code anywhere in your service.)P.S. to stop the service from being in foreground simply use
stopForeground(true);anywhere in your service