• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Retrieve incoming call

jakob.d

Lurker
I would like to retrieve the incoming call's phonenumber and do something with it like the do in Caller ID by WhitePages: A New Android App that Puts Telemarketers on Alert!

Currently my code looks like below. When I place the call the CustomBroadcastReceiver catches it and the log message is printed out. I can retrieve the telephone number from the bundle. But! I can't get the CustomPhoneStateListener to work. As you can see I have registered my customPhoneState listener to the receiver but the log message never get's printed out from the CustomPhoneStateListener class. What am I my missing here? Is my thinking correct?
Code:
<receiver android:name=".CustomBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />     
        </intent-filter>
</receiver>

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Code:
public class CustomPhoneStateListener extends PhoneStateListener {

private static final String TAG = "CustomPhoneStateListener";

public void onCallStateChange(int state, String incomingNumber){

        Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
        Log.v(TAG, incomingNumber);

        switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                        Log.d(TAG, "RINGING");
                        break;
        }       
}
Code:
public class CustomBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "CustomBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();

    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);


    Bundle bundle = intent.getExtras();
    String phoneNr= bundle.getString("incoming_number");
        Log.v(TAG, "phoneNr: "+phoneNr);
}
 
In your CustomPhoneStateListener class you have:

public void onCallStateChange

Try changing to:

@Override
public void onCallStateChanged

I'm trying the same functionality myself. If I can get it to work, I'll post an update.
 
Works for me now. Your original code, with the modification from my last post, and I now see the log messages from the CustomPhoneStateListener.

Thanks for the head start!
 
Hello,

very helpfull thread. I succesfully retrieve the incoming number and store it to a file :)

I have a related question:
Is there some flag or something which shows if the call is forwarded (redirected) ?

Best Regards
 
Back
Top Bottom