kashili kashili
kashili kashili

Reputation: 955

How to read the number of incoming call on Android Device

This question was asked several times but none of the solutions are working for me. I am on kitkat(4.4.4)/Nexus5. Below are solutions I tried

1)Based on How to detect incoming calls, in an Android device? solution, I tried to retrieve the number from "extras". But intent.getExtras.getString always returns null

2)Based on Get phone number of present incoming and outgoing call solution, I tried to read from the log, but "cur.getString(0)", always returns null

I am NOT using TelephonyManager any where in my code (though few in above links seem to be using telephonymanager). I am using BroadcastReceiver. My code is below. I ended up trying above 2 solutions, because I am getting null value(callingNumber) in my callback below.

  public class MyPhoneStateListener extends PhoneStateListener {
        //blah blah
    public void onCallStateChanged(int state, String callingNumber) 
       {
          //callingNumber is always null, unfortunately
        } 
   }

Upvotes: 0

Views: 154

Answers (1)

Sergiu Giurgica
Sergiu Giurgica

Reputation: 116

For me this work, try and tell me

import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.packet.DataForm;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyIncomingCallListenerService extends BroadcastReceiver {

private StringBuffer incomingCall = new StringBuffer();
@Override
public void onReceive(Context context, Intent intent) {

try {
    TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

    tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

} catch (Exception e) {
   e.printStackTrace();
}

}

private class MyPhoneStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);


        }
    }
}


}

Upvotes: 2

Related Questions