Android, TelephonyManager, the joys of PhoneStateListener and incoming numbers Android, TelephonyManager, the joys of PhoneStateListener and incoming numbers android android

Android, TelephonyManager, the joys of PhoneStateListener and incoming numbers


You probably need to make use of broadcast receiver which may help you what you trying to achieve. create class extending broadcast receiver and in that try to catch the incoming number.

public class MyReceiver extends BroadcastReceiver {    @Override    public void onReceive(final Context context, Intent intent) {        TelephonyManager mtelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);        mtelephony.listen(new PhoneStateListener(){            @Override            public void onCallStateChanged(int state, String incomingNumber) {                super.onCallStateChanged(state, incomingNumber);               switch (state) {                 case TelephonyManager.CALL_STATE_RINGING:                // CALL_STATE_RINGING                Log.d("MyLittleDebugger", "I'm in " + state + " and the number is " + incomingNumber);            Toast.makeText(getApplicationContext(), incomingNumber,                    Toast.LENGTH_LONG).show();            Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",                    Toast.LENGTH_LONG).show();            break;        default:            break;               }               }        },PhoneStateListener.LISTEN_CALL_STATE);    }

and in your manifest this line as well.

        <receiver android:name=".MyReceiver" >            <intent-filter>                <action android:name="android.intent.action.PHONE_STATE" />            </intent-filter>        </receiver>


I haven't used the listen function of TelephonyManager, but I did successfully use a BroadcastReceiver for getting the phone state changes and the phone number.

Register your receiver in the Activity or in the Manifest (if you want to receive updates when the app is in background):

Activity:

@Overrideprotected void onResume() {     super.onResume();     BroadcastReceiver receiver = new PhoneStateBroadcastReceiver();     IntentFilter filter= new IntentFilter();     filter.addAction("android.intent.action.PHONE_STATE");     filter.addAction("android.intent.action.NEW_OUTGOING_CALL");     registerReceiver(reciever, filter);}@Overrideprotected void onPause() {    super.onPause();    unregisterReceiver(reciever); }  

Manifest:

<receiver    android:name=".PhoneStateBroadcastReceiver"    android:permission="android.permission.READ_PHONE_STATE" >    <intent-filter>        <action android:name="android.intent.action.PHONE_STATE" />        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />    </intent-filter></receiver>

and a basic receiver:

public class PhoneStateBroadcastReceiver extends BroadcastReceiver {   private final String TAG = getClass().getName();   private static String number = null;   @Override   public void onReceive(final Context context, final Intent intent) {      if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {         String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);         Log.d(TAG, intent.getAction() + ", EXTRA_STATE: " + state);         // on ringing get incoming number         if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {            number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);            Log.d(TAG, "EXTRA_INCOMING_NUMBER: " + number);         }      }      if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {         number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);         Log.d(TAG, intent.getAction() + ", EXTRA_PHONE_NUMBER: " + number);      }   }}

and in this SO answer you can find a nice implementation that also handles multiple calls:https://stackoverflow.com/a/15564021/348378


I would advise you to run your app on a real working phone!

There are multiple reasons why a phone number is not available on all notifications, but you stand a much better chance of there being one if the call is from a real phone network that can provide the number.