title: Android: Status bar Notifications using a Service
body: <p>In one of my Activities <strong>Activity_A</strong> i have overrode <strong>onResume()</strong> this way:</p>
  
  <pre><code>protected void onResume() {
  
  super.onResume();
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent i = new Intent(this, NotificationsService.class);
  PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
  am.cancel(pi);
  am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 20000, 20000, pi);
  }
  </code></pre>
  
  <p>To start a Notification after 20 secs and then repeat it every 20 secs.</p>
  
  <p>But this is not working, i am not getting any Notification even after 5-10 mins.What am i missing.</p>
  
  <p>NotificationService Class:</p>
  
  <pre><code>public class NotificationsService extends Service {
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          handleIntent(intent);
          return START_NOT_STICKY;
      }
  
      private NotificationManager nm;
      private WakeLock mWakeLock;
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          mWakeLock.release();
      }
  
      private void showNotification() {
  
          nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          Notification notification = new Notification(R.drawable.icon,
                  "This is a Notification", System.currentTimeMillis());
          notification.flags = Notification.FLAG_AUTO_CANCEL;
          Intent i = new Intent(this, Activity_B.class);
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i,
                  PendingIntent.FLAG_UPDATE_CURRENT);
          notification.setLatestEventInfo(this, "New Notifications",
                  "Notification Content", contentIntent);
          nm.notify(R.string.app_name, notification);
      }
  
      private class PollTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
              showNotification();
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
  
              stopSelf();
          }
      }
  
      private void handleIntent(Intent intent) {
  
          PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
          mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                  "NotificationsService");
          mWakeLock.acquire();
          ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
          if (!cm.getBackgroundDataSetting()) {
              stopSelf();
              return;
          }
          new PollTask().execute();
      }
  }
  </code></pre>
  
  <p>and my BroadcastService class to call this Notification is:</p>
  
  <pre><code>public class MyScheduleReceiver extends BroadcastReceiver {
  
      private static final long REPEAT_TIME = 60000;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          AlarmManager service = (AlarmManager) context
                  .getSystemService(Context.ALARM_SERVICE);
          Intent i = new Intent(context, NotificationsService.class);
          PendingIntent pending = PendingIntent.getService(context, 0, i,
                  PendingIntent.FLAG_CANCEL_CURRENT);
          service.cancel(pending);
          service.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                  SystemClock.elapsedRealtime() + REPEAT_TIME, REPEAT_TIME, pending);
      }
  }
  </code></pre>
  
  <p>and i have included this in the Manifest.xml as:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.Android.WiC_MobileApp"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="10"
          android:targetSdkVersion="14" /&gt;
  
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
      &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
  
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Light" &gt;
          &lt;activity
              android:name=".Activity_A"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name"
              android:screenOrientation="portrait" &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=".Activity_B"
              android:configChanges="orientation|keyboardHidden"
              android:excludeFromRecents="true"
              android:label="@string/app_name"
              android:launchMode="singleTask"
              android:screenOrientation="portrait"
              android:taskAffinity="" /&gt;
  
          &lt;service
              android:name="NotificationsService"
              android:icon="@drawable/icon"
              android:label="Notifications"
              android:process=":my_process" &gt;
          &lt;/service&gt;
  
          &lt;receiver android:name=".MyScheduleReceiver" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Thank You</p>