title: Android AlarmManager not working while phone asleep
body: <p>I've got a problem with AlarmManager.</p>
  
  <p>In short, I plan an alarmManager :</p>
  
  <pre><code>Intent intent = new Intent(context, MyActivity.class);
  PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayInMs, pendingIntent);
  </code></pre>
  
  <p>And the activity MyActivity appears at the specified time. Just when the device is plugged. It also works when it is in my pocket, or when the delay is a few minutes.
  But when I set the alarmManager before night, it won't work in the morning. However, it will work as soon as I take the phone or unlock the screen.</p>
  
  <p>So, I suppose it is due to a sleep mode of the device, but how solve this ?</p>
  
  <p>1) I added a log in every methods of myActivity, and I'm sure no one is called before I manually wake the device.
  2) I tried PowerManagement's wake lock (with the WAKE_LOCK permission in manifest), but nothing changed :</p>
  
  <pre><code>alarmManager.setExact(.........);
  wakeLock = ((PowerManager)contexte.getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "MyActivity");
  wakeLock.acquire();
  </code></pre>
  
  <p>Please, help ! I'm sure I'm so close...</p>
  
  <p>EDIT Dec 04'16 :
  Thanks to Nick Friskel and Vikram Rao, I changed my initial code to call a broadcastReceiver and acquire my wakeLock in the onReceive. Unfortunately, it doesn't seem to work. It perfectly works when the phone is plugged or when the alarm is planned 35 minutes later, but for a complete night, the onReceive isn't even called.
  I tried that night, with an alarm planned at 9:00 AM, but the onReceive was only executed at 9:46 AM, that means the moment where I unlocked the device.
  Here's my new code :</p>
  
  <pre><code>Intent intent = new Intent("com.blah.something.ALARM_RECEIVED");
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  alarmManager.cancel(pendingIntent);
  alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delayInMs, pendingIntent);
  </code></pre>
  
  <p>That said, my log writing "start of onReceive" wasn't really at the beginning of the listener for some reason. I just managed to put it at the real beginning, so I'll see if the listener is called or not.</p>
  
  <p>EDIT Dec 05'16 :
  So, I changed the log writing, at the top of the onReceive, and the same problem happened : the start of the onReceive is called as soon as I manually wake the device.
  I can implement the wakefulBroadcastReceiver, but I fear it won't solve anything. If I understood correctly, the wakefulBroadcastReceiver is useful to prevent the device to sleep between onReceive and the launch of the activity or service. But what if the onReceive isn't even called ?
  I'm a bit desperate... Maybe should I directly ask Sony.
  Furthermore, my phone has stamina mode, but it isn't activated.</p>
  
  <p><b>EDIT Dec 11'16 :</b>
  So, with more tests, I'm now sure that I understand nothing.... I set a broadcastReceiver which activates every 5 minutes (the onReceive resets the alarmManager 5 minutes later), and I can see it's perfectly working... sometimes. It can last several hours, and sleep for two hours, then ok for 30 minutes, then back to sleep. (all that when my phone is on, unplugged and idle).
  I'm going to remove ALL the code but what interests us. It will be easier to understand and I will be able to write here all the active code.</p>
  
  <p>AndroidManifest.xml</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.par.hasard.mysimpleapplication"&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&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;receiver android:name="com.par.hasard.mysimpleapplication.MySimpleReceiver"&gt;
              &lt;intent-filter android:priority="1"&gt;
                  &lt;action android:name="com.par.hasard.mysimpleapplication.REGULAR_ALARM" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.par.hasard.mysimpleapplication;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Button button = (Button)findViewById(R.id.myExportButton);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  MyLogManager.copyLogToClipboard(view.getContext());
                  MyLogManager.emptyLogFile(view.getContext());
              }
          });
          try {
              MyLogManager.createLogFile(this);
              MyLogManager.write(this, "Application launched\n");
              MyAlarmPlanner.planAlarm(this);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>MySimpleReceiver.java</p>
  
  <pre><code>package com.par.hasard.mysimpleapplication;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  
  public class MySimpleReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          try {
              MyLogManager.write(context, "Beginning of onReceive\n");
              MyAlarmPlanner.planAlarm(context);
              MyLogManager.write(context, "End of onReceive\n");
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>MyAlarmPlanner.java</p>
  
  <pre><code>package com.par.hasard.mysimpleapplication;
  
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.os.SystemClock;
  import java.io.IOException;
  
  public class MyAlarmPlanner {
      public static void planAlarm(Context context) throws IOException {
          MyLogManager.write(context, "Beginning of alarm planning\n");
          AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent("com.par.hasard.mysimpleapplication.REGULAR_ALARM");
          PendingIntent pendingIntent =  PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
          alarmManager.cancel(pendingIntent);
          alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 300000, pendingIntent);
          MyLogManager.write(context, "End of alarm planning\n");
      }
  }
  </code></pre>
  
  <p>I don't think MyLogManager.java is useful, it's just boring files management methods.</p>
  
  <p>Content of log file after a long idle time :</p>
  
  <pre><code>12/12 15h33m23s380 =&gt; Beginning of onReceive
  12/12 15h33m23s381 =&gt; Beginning of alarm planning
  12/12 15h33m23s383 =&gt; End of alarm planning
  12/12 15h33m23s384 =&gt; End of onReceive
  12/12 15h38m24s337 =&gt; Beginning of onReceive
  12/12 15h38m24s339 =&gt; Beginning of alarm planning
  12/12 15h38m24s375 =&gt; End of alarm planning
  12/12 15h38m24s376 =&gt; End of onReceive
  12/12 15h43m24s375 =&gt; Beginning of onReceive
  12/12 15h43m24s376 =&gt; Beginning of alarm planning
  12/12 15h43m24s380 =&gt; End of alarm planning
  12/12 15h43m24s381 =&gt; End of onReceive
  12/12 15h48m25s301 =&gt; Beginning of onReceive
  12/12 15h48m25s304 =&gt; Beginning of alarm planning
  12/12 15h48m25s307 =&gt; End of alarm planning
  12/12 15h48m25s308 =&gt; End of onReceive
  12/12 15h53m25s316 =&gt; Beginning of onReceive
  12/12 15h53m25s318 =&gt; Beginning of alarm planning
  12/12 15h53m25s328 =&gt; End of alarm planning
  12/12 15h53m25s329 =&gt; End of onReceive
  12/12 15h58m25s328 =&gt; Beginning of onReceive
  12/12 15h58m25s329 =&gt; Beginning of alarm planning
  12/12 15h58m25s331 =&gt; End of alarm planning
  12/12 15h58m25s333 =&gt; End of onReceive
  12/12 16h3m26s336 =&gt; Beginning of onReceive
  12/12 16h3m26s351 =&gt; Beginning of alarm planning
  12/12 16h3m26s379 =&gt; End of alarm planning
  12/12 16h3m26s380 =&gt; End of onReceive
  12/12 16h8m26s397 =&gt; Beginning of onReceive
  12/12 16h8m26s401 =&gt; Beginning of alarm planning
  12/12 16h8m26s404 =&gt; End of alarm planning
  12/12 16h8m26s405 =&gt; End of onReceive
  12/12 16h13m26s406 =&gt; Beginning of onReceive
  12/12 16h13m26s407 =&gt; Beginning of alarm planning
  12/12 16h13m26s410 =&gt; End of alarm planning
  12/12 16h13m26s411 =&gt; End of onReceive
  12/12 16h18m27s328 =&gt; Beginning of onReceive
  12/12 16h18m27s329 =&gt; Beginning of alarm planning
  12/12 16h18m27s346 =&gt; End of alarm planning
  12/12 16h18m27s348 =&gt; End of onReceive
  12/12 16h23m28s298 =&gt; Beginning of onReceive
  12/12 16h23m28s299 =&gt; Beginning of alarm planning
  12/12 16h23m28s303 =&gt; End of alarm planning
  12/12 16h23m28s304 =&gt; End of onReceive
  12/12 16h28m29s308 =&gt; Beginning of onReceive
  12/12 16h28m29s310 =&gt; Beginning of alarm planning
  12/12 16h28m29s323 =&gt; End of alarm planning
  12/12 16h28m29s324 =&gt; End of onReceive
  12/12 16h33m29s339 =&gt; Beginning of onReceive
  12/12 16h33m29s340 =&gt; Beginning of alarm planning
  12/12 16h33m29s355 =&gt; End of alarm planning
  12/12 16h33m29s361 =&gt; End of onReceive
  12/12 16h38m29s356 =&gt; Beginning of onReceive
  12/12 16h38m29s357 =&gt; Beginning of alarm planning
  12/12 16h38m29s360 =&gt; End of alarm planning
  12/12 16h38m29s361 =&gt; End of onReceive
  12/12 16h43m29s364 =&gt; Beginning of onReceive
  12/12 16h43m29s365 =&gt; Beginning of alarm planning
  12/12 16h43m29s367 =&gt; End of alarm planning
  12/12 16h43m29s369 =&gt; End of onReceive
  12/12 16h48m29s376 =&gt; Beginning of onReceive
  12/12 16h48m29s380 =&gt; Beginning of alarm planning
  12/12 16h48m29s390 =&gt; End of alarm planning
  12/12 16h48m29s394 =&gt; End of onReceive
  12/12 16h53m29s392 =&gt; Beginning of onReceive
  12/12 16h53m29s394 =&gt; Beginning of alarm planning
  12/12 16h53m29s402 =&gt; End of alarm planning
  12/12 16h53m29s403 =&gt; End of onReceive
  12/12 17h43m33s986 =&gt; Beginning of onReceive      //problem, the 16'58 onReceive wasn't called
  12/12 17h43m33s988 =&gt; Beginning of alarm planning
  12/12 17h43m33s996 =&gt; End of alarm planning
  12/12 17h43m34s4 =&gt; End of onReceive
  12/12 17h48m34s535 =&gt; Beginning of onReceive
  12/12 17h48m34s536 =&gt; Beginning of alarm planning
  12/12 17h48m34s539 =&gt; End of alarm planning
  12/12 17h48m34s540 =&gt; End of onReceive
  12/12 18h29m49s635 =&gt; Beginning of onReceive     //the moment I turned on my device
  12/12 18h29m49s648 =&gt; Beginning of alarm planning
  12/12 18h29m49s667 =&gt; End of alarm planning
  12/12 18h29m49s668 =&gt; End of onReceive
  </code></pre>
  
  <p>Can someone tell me where is my mistake ?</p>