Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement startForeground method in Android

Tags:

android

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?

like image 696
Jovan Avatar asked Sep 10 '10 18:09

Jovan


People also ask

What is startForeground in Android?

A started service can use the startForeground(int, android. app. Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory.

How can you convert a background service to a foreground service?

Inside the service, usually in onStartCommand() , you can request that your service run in the foreground. To do so, call startForeground() . This method takes two parameters: a positive integer that uniquely identifies the notification in the status bar and the Notification object itself.

How can I make a service run continuously on Android?

1: You have to invoke the service's startForeground() method within 5 seconds after starting the service. To do this, you can call startForeground() in onCreate() method of service. public class AppService extends Service { .... @Override public void onCreate() { startForeground(9999, Notification()) } .... }


2 Answers

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

like image 53
Someone Somewhere Avatar answered Oct 23 '22 17:10

Someone Somewhere


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

like image 34
Abandoned Cart Avatar answered Oct 23 '22 16:10

Abandoned Cart