Question: So I'm not sure where/how to implement this method to make my service run in the foreground. Currently I start my service by the following in another activity: 

```
Intent i = new Intent(context, myService.class); 
context.startService(i);

```

And then in myServices' onCreate() I try the startForeground()...? 

```
Notification notification = new Notification();
startForeground(1, notification);

```

So yeah I'm a bit lost and unsure of how to implement this.

Answer: 
This is my code to set the service to foreground:

```
private void runAsForeground(){
    Intent notificationIntent = new Intent(this, RecorderMainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notification=new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentText(getString(R.string.isRecording))
                                .setContentIntent(pendingIntent).build();

    startForeground(NOTIFICATION_ID, notification);

}

```

I need to build a notification using PendingIntent, so that I can start my main activity from the notification.

To remove the notification, just call the stopForeground(true);

It is called in the onStartCommand(). Please refer to my code at : <https://github.com/bearstand/greyparrot/blob/master/src/com/xiong/richard/greyparrot/Mp3Recorder.java>
If you want to make IntentService a Foreground Service
------------------------------------------------------

then you should override `onHandleIntent()`like this

```
Override
protected void onHandleIntent(@Nullable Intent intent) {

    startForeground(FOREGROUND_ID,getNotification());     //<-- Makes Foreground

   // Do something

    stopForeground(true);                                // <-- Makes it again a normal Service                         

}

```

How to make notification ?
--------------------------

simple. Here is the `getNotification()` Method

```
public Notification getNotification()
{

    Intent intent = new Intent(this, SecondActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

    NotificationCompat.Builder foregroundNotification = new NotificationCompat.Builder(this);
    foregroundNotification.setOngoing(true);

    foregroundNotification.setContentTitle("MY Foreground Notification")
            .setContentText("This is the first foreground notification Peace")
            .setSmallIcon(android.R.drawable.ic_btn_speak_now)
            .setContentIntent(pendingIntent);

    return foregroundNotification.build();
}

```

Deeper Understanding
--------------------

### What happens when a service becomes a foreground service

> 
> This happens
> 
> 
> 

[![enter image description here](https://i.stack.imgur.com/6ve6N.jpg)](https://i.stack.imgur.com/6ve6N.jpg)

What is a foreground Service ?
------------------------------

A foreground service,

* makes sure that user is actively aware of that something is going on in the background
by providing the notification.
* (most importantly) is not killed by System when it runs low on memory

### A use case of foreground service

[Implementing song download functionality in a Music App](https://github.com/rohitksingh/OLA_Play_Music_App)