Service with START_STICKY
<p>I want to create service which will check new notifications every some time, even if main app is closed.</p>

<p>Add to manifest</p>

<pre><code>    &lt;service
        android:name=".service.NotificationService"
        android:enabled="true"
        android:exported="true"
        android:stopWithTask="false"
        android:process=":notification"/&gt;

    &lt;!-- Declaring broadcast receiver for BOOT_COMPLETED event. --&gt;
    &lt;receiver android:name=".BootReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
        &gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt;
            &lt;category android:name="android.intent.category.DEFAULT" /&gt;

        &lt;/intent-filter&gt;
    &lt;/receiver&gt;
</code></pre>

<p>Second is boot receiver, it also will start service after reboot.</p>

<p>NotificationService.class</p>

<pre><code>import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.util.Log;

import com.tagwishes.fc.Notification;
import com.tagwishes.fc.app.AppConfig;
import com.tagwishes.fc.helper.SessionManager;

import java.util.Timer;
import java.util.TimerTask;

public class NotificationService extends Service {


    static int WIFI_NETWORK_ID = 1;
    static int MOBILE_NETWORK_ID = 2;
    static int NO_NETWORK_ID = 3;
    private SessionManager sm;


    final String LOG_TAG = "fc";

    public void onCreate() {
        super.onCreate();
        Log.d(LOG_TAG, "MyService onCreate");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d(LOG_TAG, "MyService onDestroy");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, "MyService onStartCommand");


        readFlags(flags);
        MyRun mr = new MyRun(startId);
        new Thread(mr).start();


        Log.d("fc", "START_STICKY");

        return START_STICKY;
    }

    public IBinder onBind(Intent arg0) {
        return null;
    }

    int chkStatus() {
        final ConnectivityManager connMgr = (ConnectivityManager)
                this.getSystemService(Context.CONNECTIVITY_SERVICE);
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi.isConnectedOrConnecting()) {
            return WIFI_NETWORK_ID;
        } else if (mobile.isConnectedOrConnecting()) {
            return MOBILE_NETWORK_ID;
        } else {
            return NO_NETWORK_ID;
        }
    }

    void readFlags(int flags) {
        if ((flags&amp;START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY)
            Log.d(LOG_TAG, "START_FLAG_REDELIVERY");
        if ((flags&amp;START_FLAG_RETRY) == START_FLAG_RETRY)
            Log.d(LOG_TAG, "START_FLAG_RETRY");
    }

    class MyRun implements Runnable {

        int startId;

        public MyRun(int startId) {
            this.startId = startId;
            Log.d(LOG_TAG, "MyRun#" + startId + " create");
        }

        public void run() {
            Log.d(LOG_TAG, "MyRun#" + startId + " start");

            final int networkID = chkStatus();

            Log.d("fc", "networkID " + networkID);

            if (sm == null) {
                sm = new SessionManager(getApplication());
            }

            int reCheckTime = AppConfig.NOTIFICATION_RECHECK_DEFAULT;

            if (networkID == WIFI_NETWORK_ID) {
                reCheckTime = AppConfig.NOTIFICATION_RECHECK_WIFI;
            } else if (networkID == MOBILE_NETWORK_ID) {
                reCheckTime = AppConfig.NOTIFICATION_RECHECK_MOBILE;
            }


            final Notification nt = new Notification(getApplicationContext());//Its my own class


            Log.d("fc", "Timer check noti started " + reCheckTime);
            new Timer().scheduleAtFixedRate(new TimerTask(){
                @Override
                public void run(){

                    if (networkID != NO_NETWORK_ID || !sm.isLoggedIn()) {//IF HAVE NETWORK CONNECTION
                        Log.d("fc", "checkNotifications ");

                        nt.checkNotifications();
                    }

                }
            }, 0, reCheckTime*1000);


        }

    }

}
</code></pre>

<p>Boot Receiver</p>

<pre><code>public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.d("fc", "BootReceiver BroadcastReceiver");

    Intent myIntent = new Intent(context, NotificationService.class);
    context.startService(myIntent);
}
</code></pre>

<p>}</p>

<p>And this how i call service on MainActivity</p>

<pre><code>        Intent bindIntent = new Intent(getBaseContext(), NotificationService.class);
        startService(bindIntent);
</code></pre>

<p>When i close app from Apps History, it close also my :notification process, and service not started again. I use START_STICKY for this.</p>