Background service using alarm manager
<p>I have followed below example code for implementing Periodic background service. Periodic task executes correctly If app on foreground on 1st time. If, I close application then Background service is not working. </p>

<p><a href="https://github.com/codepath/android_guides/wiki/Starting-Background-Services#using-with-alarmmanager-for-periodic-tasks" rel="nofollow noreferrer">https://github.com/codepath/android_guides/wiki/Starting-Background-Services#using-with-alarmmanager-for-periodic-tasks</a></p>

<p>1) BroadcastReceiver service for Bootcomplete</p>

<pre><code>public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    // Launch the specified service when this message is received
    Intent startServiceIntent = new Intent(context, MyTestService.class);
    context.startService(startServiceIntent);
}
}
</code></pre>

<p>2) BroadcastReceiver to start service</p>

<pre><code>public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;

// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, MyTestService.class);
    i.putExtra("foo", "bar");
    context.startService(i);
    Toast.makeText(context, "Welcome --------1", Toast.LENGTH_LONG).show();
}
}
</code></pre>

<p>3) IntenetService class: To execute my background task</p>

<pre><code>public class MyTestService extends IntentService {


// Must create a default constructor
public MyTestService() {
    // Used to name the worker thread, important only for debugging.
    super("test-service");
}

@Override
public void onCreate() {
    super.onCreate(); // if you override onCreate(), make sure to call super().
}

@Override
protected void onHandleIntent(Intent intent) {
}   
}
</code></pre>

<p>4) Service is started from activity as</p>

<pre><code>public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scheduleAlarm();
}

// Setup a recurring alarm every half hour
public void scheduleAlarm() {
    // Construct an intent that will execute the AlarmReceiver
    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every 5 seconds
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
            30000, pIntent);
}
}
</code></pre>

<p>5) Manifest file</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="test.org.help"&gt;

&lt;permission
    android:name="test.org.help.permission.C2D_MESSAGE"
    android:protectionLevel="signature" /&gt;

&lt;uses-permission android:name="au.com.KPS.companion.ACCESS_DATA" /&gt;
&lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
&lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
&lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
&lt;uses-permission android:name="android.permission.CAMERA" /&gt;
&lt;uses-permission android:name="android.permission.FLASHLIGHT" /&gt;
&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
&lt;uses-permission android:name="test.org.help.permission.UA_DATA" /&gt;
&lt;!-- GCM requires a Google account. --&gt;
&lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
&lt;!-- This app has permission to register with GCM and receive message --&gt;
&lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
&lt;uses-permission android:name="test.org.help.permission.C2D_MESSAGE" /&gt;

&lt;uses-feature
    android:name="android.hardware.camera"
    android:required="false" /&gt;
&lt;uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" /&gt;
&lt;uses-feature
    android:name="android.hardware.camera.flash"
    android:required="false" /&gt;
&lt;uses-feature android:name="android.hardware.screen.landscape" /&gt;

&lt;application
    android:name="test.org.help.KPSApp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:logo="@drawable/ic_actionbar_logo"
    android:theme="@style/AppTheme"
    tools:replace="android:icon, android:logo"&gt;

    &lt;!-- Activities --&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;

            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
    &lt;activity
        android:name=".ui.activity.JanRainLoginActivity"
        android:screenOrientation="portrait" /&gt;
    &lt;activity
        android:name=".ui.activity.LinkListActivity"
        android:screenOrientation="portrait" /&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.KPSFragmentActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.NotificationDialogActivity"
        android:excludeFromRecents="true"
        android:label=""
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:screenOrientation="portrait"
        android:taskAffinity=""
        android:theme="@style/AppTheme.Dialog.Notification"&gt;&lt;/activity&gt;

    &lt;receiver
        android:name="test.org.help.receivers.NotificationReceiver"
        tools:ignore="ExportedReceiver"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="test.org.help.util.ACTION_SHOW_AGENDA" /&gt;
            &lt;action android:name="test.org.help.util.ACTION_SNOOZE" /&gt;
            &lt;action android:name="test.org.help.util.ACTION_DISMISS" /&gt;
            &lt;action android:name="test.org.help.util.ACTION_SHOW_DIALOG" /&gt;

            &lt;data android:scheme="KPS" /&gt;
            &lt;data android:mimeType="text/plain" /&gt;
            &lt;data android:pathPattern="au\.org\.KPS\.util/.*" /&gt;
        &lt;/intent-filter&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;activity
        android:name="com.google.zxing.client.android.CaptureActivity"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Light.NoTitleBar"
        tools:replace="android:theme,android:screenOrientation"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.AppTutorialActivity"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.TermsAndConditionsActivity"
        android:label="@string/title_terms_and_conditions"
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.WarningActivity"
        android:label="@string/title_Warning"
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.DifferentUserWarningActivity"
        android:label="@string/title_Warning"
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;
    &lt;activity
        android:name="test.org.help.ui.activity.AppWhatsNewActivity"
        android:label="@string/title_Warning"
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;

    &lt;activity
        android:name=".ui.activity.UserUpgradeActivity"
        android:label="@string/title_upgrade"
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;

    &lt;activity
        android:name="eu.janmuller.android.simplecropimage.CropImage"
        android:label=""
        android:screenOrientation="portrait"&gt;&lt;/activity&gt;

    &lt;!-- Facebook --&gt;
    &lt;meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" /&gt;

    &lt;!-- Services --&gt;

    &lt;receiver android:name="com.mobileapptracker.Tracker"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="com.android.vending.INSTALL_REFERRER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;service
        android:name="test.org.help.service.DBUpdateService"
        android:exported="false"
        android:label="@string/app_name"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="test.org.help.service.DBUpdateService"&gt;&lt;/action&gt;
        &lt;/intent-filter&gt;
    &lt;/service&gt;

    &lt;receiver android:name="test.org.help.receivers.TimeChangeReceiver"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.TIMEZONE_CHANGED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;


    &lt;activity
        android:name="com.janrain.android.engage.ui.JRFragmentHostActivity"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize|stateHidden" /&gt;
    &lt;activity
        android:name="com.janrain.android.engage.ui.JRFragmentHostActivity$Fullscreen"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustResize|stateHidden" /&gt;

    &lt;!--Hockey app crash report--&gt;
    &lt;meta-data
        android:name="net.hockeyapp.android.appIdentifier"
        android:value="@string/hockey_app_id" /&gt;

    &lt;receiver android:name="test.org.help.syncmanager.Network.NetworkAvailability"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=".android.action.broadcast" /&gt;
            &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
            &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;activity
        android:name=".db.AndroidDatabaseManager"
        android:theme="@style/Theme.AppCompat.Light" /&gt;&lt;!-- ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. --&gt;
    &lt;meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" /&gt;
    &lt;service
        android:name=".syncmanager.auto.MyTestService"
        android:exported="false" /&gt;

    &lt;receiver
        android:name=".syncmanager.auto.MyAlarmReceiver"
        android:process=":remote"&gt;&lt;/receiver&gt;

    &lt;receiver android:name=".syncmanager.auto.BootBroadcastReceiver"&gt;

    &lt;/receiver&gt;
&lt;/application&gt;

&lt;/manifest&gt;
</code></pre>

<p>If, implement above as sample project as separate then, background service is executing fine always. But If try to integrate with my existing code then not working...</p>

<p><strong>Edited: Found Solution</strong>
It got resolved by adding full path android:name service in AndroidManifest.xml</p>

<pre><code>&lt;service
    android:name="test.org.help.syncmanager.auto.MyTestService"
    android:exported="false" /&gt;
</code></pre>