0

I have a BroadcastReceiver that listens for the calls and when they end the AlertDialog 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.

Anyone know what this problem is about?

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 && incomingNumber.length() > 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==>"+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 && !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;
        }
    }
}

}

Manifest:

<receiver android:name=".postcall.PhoneStateBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE">
            </action>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>
jean d'arme
  • 4,033
  • 6
  • 35
  • 70
  • This has nothing to do with doze mode, as doze mode only comes up if no user action is on your device. Your dialog appears because a broadcast is send, am I right? If you have a huawei device, you have to enable autostart, otherwise no broadcasts will be send. If this is not the case, please show us your relevant part of code. – Opiatefuchs Nov 09 '16 at 20:23
  • it showed on HTC ONE M8 GPE Android 6.0. I edited my question with relevant code. – jean d'arme Nov 09 '16 at 20:30

0 Answers0