I am trying to get phone number during calling state in android with two ways. Here are the codes: The first way is
public void onReceive(Context context, Intent intent)
{
mContext = context;
mIntent = intent;
Bundle bundle = intent.getExtras();
mPhoneNumber= bundle.getString("incoming_number");
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
And the second way is
public class CustomPhoneStateListener extends PhoneStateListener
{
//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
Log.d("Number", incomingNumber);
}
}
But, i can't get phone number with 2 ways. When I executed these codes, mPhoneNumber
is null in the first way and also incomingNumber
is null in the second way.
I followed sample codes from
How to get phone number from an incoming call?.
Please help me what I got wrong?