I want to implement the startForeground()
method in the Service
class for prevent service self kill.
Can anybody sent me code for implement
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.
final static int myID = 1234;
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, SomeActivityToLaunch.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
//This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis());
//This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);
notice.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notice);
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
This code will use the best option available for any API by adding the Android support library to your project. In Eclipse, you would right click the project, go to Android Tools, and click the "Add Support Library..." option to download and add it.
final static int myID = 1234;
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, SomeActivityToLaunch.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.DONUT) {
// Build.VERSION.SDK_INT requires API 4 and would cause issues below API 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("TICKER").setContentTitle("TITLE").setContentText("CONTENT")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setOngoing(true).setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendIntent);
Notification notification = builder.build();
} else {
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis());
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);
}
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notification);
Put the code in the start method you are using for your service, then when you want the foreground process to stop, call stopForeground(true);
anywhere in your service
http://developer.android.com/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29
I'm not exactly sure if this is what you're looking for, but all you have to do is create a notification object (mOngoingNote
below) and call startForeground
using a notification ID along with the actual notification.
startForeground(NOTEMAN_ID, mOngoingNote);