title: Activity start from sleep mode immediately close?
body: <p>I have an app wich is a kind of <strong>alarmclock</strong>. I have a weird issue.</p>
  
  <p>The principle is simple : The alarmmanager send a <strong>broadcast</strong> that will <strong>start a service and an activity.</strong></p>
  
  <p>The service vibrates the phone for 10s and then kill the activity and itself.
  The activity shows a dismiss button. If you click on it, it stops the service and itself.</p>
  
  <p>If the broadcast is received when <strong>the phone is on, then it works fine</strong> (activity starts and the phone vibrates).</p>
  
  <p>If the broadcast is received when the phone <strong>is in sleeping mode</strong>, <strong>then the activity starts and immediatly stops</strong> (you actually can't see it on screen unless you put a Thread.sleep somewhere). The service works fine.</p>
  
  <p>I don't understand why the activity stops just after being created ?</p>
  
  <p>In the logcat, i have these two last lines i didn't have when it's working :</p>
  
  <pre><code>06-23 18:07:58.349: I/ActivityManager(305): Displayed com.example.testproject/.AlarmeScreenOffActivity: +100ms
  06-23 18:07:58.349: I/power(305): *** set_screen_state 1
  06-23 18:07:58.359: D/kernel(145): [557082.194793] request_suspend_state: wakeup (3-&gt;0) at 557080649502843 (2014-06-23 16:07:58.358764661 UTC)
  </code></pre>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Here's the code in the receiver :</p>
  
  <pre><code>        WakeLockManager.acquireWakeLock(context);
  
          Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
          context.sendBroadcast(closeDialogs);
  
          Intent alarmeActivity = new Intent(context,
                  AlarmeScreenOffActivity.class);
          alarmeActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(alarmeActivity);
  
          Intent playAlarm = new Intent(context, AlarmeSonVibration.class);
          context.startService(playAlarm);
  </code></pre>
  
  <p>The activity :</p>
  
  <pre><code>private BroadcastReceiver mReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          if (MyBroadcastReceiver.ALARM_TIMEOUT.equals(intent.getAction())) {
              dismiss();
          }
      }
  };
  
  @Override
  protected void onCreate(Bundle icicle) {
      super.onCreate(icicle);     
      updateLayout();     
      registerReceiver(mReceiver, new IntentFilter(MyBroadcastReceiver.ALARM_TIMEOUT));
  }
  
  private void updateLayout() {   
  
      requestWindowFeature(android.view.Window.FEATURE_NO_TITLE); 
      this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | 
              WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
              WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
              WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
              WindowManager.LayoutParams.FLAG_FULLSCREEN | 
              WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
              WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
              WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
  
      setContentView(R.layout.alarm_alert);
  
      Button dismissbouton = (Button) findViewById(R.id.dismiss);
      dismissbouton.setText("Dismiss");
  
      dismissbouton.setOnClickListener(new Button.OnClickListener() {
          public void onClick(View v) {                   
              dismiss();
          }
      });
  
  }
  
  private void dismiss() {
      Intent playAlarm = new Intent(this, AlarmeSonVibration.class);
      stopService(playAlarm);
      finish();
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      unregisterReceiver(mReceiver);  
  }
  </code></pre>
  
  <p>And the service :</p>
  
  <pre><code>private static int ALARM_TIMEOUT_SECONDS = 10;
  private static final long[] sVibratePattern = new long[] { 500, 300 };
  private boolean mPlaying = false;
  private Vibrator mVibrator;
  
  private static final int TIMEOUT = 1001;    
  private Handler mHandler = new Handler() {
      public void handleMessage(Message msg) {
          switch (msg.what) {
          case TIMEOUT:
              sendTimeOutBroadcast();
              stopSelf();
              break;
          }
      }
  };
  
  @Override
  public void onCreate() {
      WakeLockManager.acquireWakeLock(this);      
      mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);      
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      stop();
      WakeLockManager.releaseWakeLock();
  }
  
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      if (intent == null) {
          stopSelf();
          return START_NOT_STICKY;
      }
      play();
      return START_STICKY;
  }
  
  private void sendTimeOutBroadcast() {
      Intent alarmtimeout = new Intent(MyBroadcastReceiver.ALARM_TIMEOUT);
      sendBroadcast(alarmtimeout);
  }
  
  private void play() {
      stop();
      mVibrator.vibrate(sVibratePattern, 0);
      enableTimeOut();
      mPlaying = true;
  }
  
  public void stop() {
      if (mPlaying) {
          mPlaying = false;
          mVibrator.cancel();
      }
      disableTimeOut();
  }
  
  private void enableTimeOut() {
      mHandler.sendMessageDelayed(mHandler.obtainMessage(TIMEOUT, true),
              1000 * ALARM_TIMEOUT_SECONDS);
  }
  
  private void disableTimeOut() {
      mHandler.removeMessages(TIMEOUT);
  }
  </code></pre>
  
  <p><strong>EDIT :</strong></p>
  
  <p>After a few other tests, it appears that even without starting the service, the app doesn't want my activity to stay on ui.</p>
  
  <p>The broadcast triggers the activity and the device wakes up and run the activity, but then it shuts down (it goes from onresume to onpause immediatly).</p>
  
  <p>With or whithout keyguard, the result is the same, my activity starts and stops.</p>
  
  <p>It should be simple ? I just try to run an activity from sleep mode by trigering a broadcast.</p>