title: Develop Alarm Application
body: <p>I'd like develop an Alarm Application.
  The application should work like this:</p>
  
  <ul>
  <li>launch it</li>
  <li>the activity show me the time</li>
  <li>I can set the alarm</li>
  <li>I can close the application</li>
  <li>when the alarm time comes , it starts an activity (even if the device is locked)</li>
  </ul>
  
  <p>I have tried to adapt this sample <a href="https://github.com/commonsguy/cwac-wakeful" rel="nofollow noreferrer">https://github.com/commonsguy/cwac-wakeful</a> but I cannot launch an activity when the  alarm time comes.  </p>
  
  <p>I use this code to setup the alarm (for test I have inserted this code on an <code>onCreate</code> method of activity):</p>
  
  <pre><code>Intent intent = new Intent(this, OnAlarmReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.SECOND, 10);
  AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
              pendingIntent);
  </code></pre>
  
  <p>this is the OnAlarmReceiver class:</p>
  
  <pre><code>public class OnAlarmReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
          WakefulIntentService.sendWakefulWork(context, AlarmService.class);
      }
  }
  </code></pre>
  
  <p>this is the service class:</p>
  
  <pre><code>public class AlarmService extends WakefulIntentService {
  
      public AlarmService(String name) {
          super(name);
      }
  
      @Override
      protected void doWakefulWork(Intent intent) {
          Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
          Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
          newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          newIntent.setAction(ClockActivity.ALARM_ACTION);
          getApplicationContext().startActivity(newIntent);
      }
  }
  </code></pre>
  
  <p>this is the portion of Manifest where are setup the service and the receiver:</p>
  
  <pre><code>&lt;receiver android:name=".OnAlarmReceiver"&gt;&lt;/receiver&gt;
  &lt;service android:name=".AlarmService"&gt;&lt;/service&gt;
  </code></pre>
  
  <p>the doWakefulWork method is never called!</p>