0

My app can't read incoming number when API is below 21. How can I make it work? I tried this approach: http://mmarvick.github.io/blog/blog/lollipop-multiple-broadcastreceiver-call-state/ but doesn't work

Code:

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "PhoneStateBroadcast";
Context mContext;
String incoming_nr;
private static int prev_state;
static CustomPhoneStateListener customPhoneListener;

@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
    if(customPhoneListener == null){
        customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    Bundle bundle = intent.getExtras();
    String phoneNr = bundle.getString("incoming_number");
    incoming_nr = phoneNr;
    Log.v(TAG, "phoneNr: " + phoneNr);
    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;
        }

        //if(prev_state == state) return;

        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){

                    Intent i = new Intent(mContext, PostCallPromptActivity.class);
                    i.putExtra(Constants.EXTRA_CALL_LOG_NUMBER, incomingNumber);
                    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;
        }
    }
}

}

jean d'arme
  • 4,033
  • 6
  • 35
  • 70
  • did you set up a minimum sdk level to include all other platforms you'd like support for? – OlaB Sep 26 '16 at 15:53
  • yes, I set up `minSdkVersion` to `18` in `build.gradle` – jean d'arme Sep 26 '16 at 15:55
  • the only thing I know I can cooment on this is that API 19 (kitkat) and above do not require appcompat libraries, so if you are extending your project to lower API's that require those resources then your app might not read it as stated. – OlaB Sep 26 '16 at 16:00
  • you might wana add the appcompat jars to your folder to see if it would support lower versions, just a suggestion – OlaB Sep 26 '16 at 16:03
  • Check out the answer [here!](http://stackoverflow.com/questions/15563921/how-to-detect-incoming-calls-in-an-android-device) Hope it helps! – Georgios Iniatis Oct 17 '16 at 06:12

0 Answers0