on device 4.4 and emulator 6.0, the code works, on 5.1 I get null instead of the phone number in the line phoneNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); when the incoming call ends

 public void onReceive(Context context, Intent intent) { if (intent.getAction().equalsIgnoreCase(INCOME_CALL_RECEIVE_ACTION)) { String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); phoneNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); if (phone_state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) { Intent intentT = new Intent(context, MyService.class); intentT.putExtra("phone", phoneNumber); intentT.putExtra(CONST_STATUS_CODE, CODE_INCOME_START); context.startService(intentT); } if (phone_state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) { Intent intentT = new Intent(context, MyService.class); intentT.putExtra("phone", phoneNumber); intentT.putExtra(CONST_STATUS_CODE, CODE_INCOME_STOP); context.startService(intentT); } } } 

in the manifest <uses-permission android:name="android.permission.READ_PHONE_STATE" /> and

  • Also in the manifest receiver -a <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> - x555xx
  • The purpose of an incoming call is to get the caller's number and send it to the service; after the missed call ends, also send the number of the calling number to the service. - x555xx
  • The reasons for mb are different, depending on which intent you accept as Receive, its limited period, how the service works, permysn in the manifest, I advise you to start with the fact that you just register the log in the Receive root and see that its behavior is interesting, I have There is an example that will work, now I will write below. - Shwarz Andrei

1 answer 1

There is a listener on the states with callback, there is a number that you need. Try this.

 public class PhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, final Intent intent) { TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephony.listen(new PhoneStateListener() { @Override public void onCallStateChanged(int state, final String number) { super.onCallStateChanged(state, number); Log.d("onCallStateChanged", " === " + number); } }, PhoneStateListener.LISTEN_CALL_STATE); } } 
  • one
    Thank! Works! - x555xx