title: Doze and BroadcastReceiver - not working together on Marshmallow
body: <p>I have a <code>BroadcastReceiver</code> that listens for the calls and when they end the <code>AlertDialog</code> appears. On Android 5.1 and lower it works all well, but on Android 6.0 (Marshmallow) and higher after some time when someone calls dialog does not appear after the call. It shows when app is manually started again and if there was more than one call - all the dialogs that should have been shown immediately are showed at once which is not good.</p>
  
  <p>Anyone know what this problem is about?</p>
  
  <p>code:</p>
  
  <pre><code>public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
  
  private static final String TAG = "PhoneStateBroadcast";
  Context mContext;
  private static String incoming_nr;
  private static int prev_state;
  static CustomPhoneStateListener customPhoneListener;
  
  @Override
  public void onReceive(Context context, Intent intent) {
      if (ContextCompat.checkSelfPermission(
              context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){
          TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
          if(customPhoneListener == null){
              customPhoneListener = new CustomPhoneStateListener();
              telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
          }
      }
  
  
      if(Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())){
          incoming_nr = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
      } else {
          Bundle bundle = intent.getExtras();
          String phoneNr = bundle.getString("incoming_number");
          if(phoneNr != null)incoming_nr = phoneNr;
      }
  
      Log.v(TAG, "phoneNr: " + incoming_nr);
      mContext = context;
  }
  
  public class CustomPhoneStateListener extends PhoneStateListener {
  
      private static final String TAG = "CustomPhoneStateListnr";
  
      @Override
      public void onCallStateChanged(int state, String incomingNumber){
          if(incomingNumber != null &amp;&amp; incomingNumber.length() &gt; 0){
              incoming_nr = incomingNumber;
          }
  
          switch(state){
              case TelephonyManager.CALL_STATE_RINGING:
                  Log.d(TAG, "CALL_STATE_RINGING");
                  prev_state = state;
                  break;
              case TelephonyManager.CALL_STATE_OFFHOOK:
                  Log.d(TAG, "CALL_STATE_OFFHOOK");
                  prev_state = state;
                  break;
              case TelephonyManager.CALL_STATE_IDLE:
                  Log.d(TAG, "CALL_STATE_IDLE==&gt;"+incoming_nr);
                  if(prev_state == TelephonyManager.CALL_STATE_OFFHOOK){
  
                      Realm realm = Realm.getDefaultInstance();
                      Contact contact = realm.where(Contact.class)
                              .contains(Constants.EXTRA_CONTACT_NUMBER, incoming_nr)
                              .findFirst();
  
                      if(contact != null &amp;&amp; !contact.getPopupFlag())
                          break;
  
                      Intent i = new Intent(mContext, PostCallPromptActivity.class);
                      i.putExtra(Constants.EXTRA_CALL_LOG_NUMBER, incoming_nr);
                      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      mContext.startActivity(i);
  
                      prev_state = state;
                      //Answered Call which is ended
                  }
                  if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                      prev_state = state;
                      //Rejected or Missed call
                  }
                  break;
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;receiver android:name=".postcall.PhoneStateBroadcastReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.PHONE_STATE"&gt;
              &lt;/action&gt;
              &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  </code></pre>