1. Are you ready for the Galaxy S20? Here is everything we know so far!

Retrieve incoming call

Discussion in 'Android Development' started by jakob.d, Dec 6, 2009.

  1. jakob.d

    jakob.d Lurker
    Thread Starter

    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 (Text):
    1.  
    2. <receiver android:name=".CustomBroadcastReceiver">
    3.         <intent-filter>
    4.                 <action android:name="android.intent.action.PHONE_STATE" />    
    5.         </intent-filter>
    6. </receiver>
    7.  
    8. </application>
    9. <uses-sdk android:minSdkVersion="5" />
    10. <uses-permission android:name="android.permission.INTERNET" />
    11. <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    12. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    13.  
    Code (Text):
    1.  
    2. public class CustomPhoneStateListener extends PhoneStateListener {
    3.  
    4. private static final String TAG = "CustomPhoneStateListener";
    5.  
    6. public void onCallStateChange(int state, String incomingNumber){
    7.  
    8.         Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    9.         Log.v(TAG, incomingNumber);
    10.  
    11.         switch(state){
    12.                 case TelephonyManager.CALL_STATE_RINGING:
    13.                         Log.d(TAG, "RINGING");
    14.                         break;
    15.         }      
    16. }
    17.  
    Code (Text):
    1.  
    2. public class CustomBroadcastReceiver extends BroadcastReceiver {
    3.  
    4. private static final String TAG = "CustomBroadcastReceiver";
    5.  
    6. @Override
    7. public void onReceive(Context context, Intent intent) {
    8.         Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    9.     TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    10.         CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
    11.  
    12.     telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    13.  
    14.  
    15.     Bundle bundle = intent.getExtras();
    16.     String phoneNr= bundle.getString("incoming_number");
    17.         Log.v(TAG, "phoneNr: "+phoneNr);
    18. }
    19.  
     


  2. tfmegatron

    tfmegatron Lurker

    Did you ever find a solution to this?
     
  3. drewashby

    drewashby Lurker

    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.
     
  4. drewashby

    drewashby Lurker

    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!
     
  5. longjohn

    longjohn Lurker

    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
     
  6. plz Upload your code for auto reply of text message to incoming call and
    :)

    retrieve the incoming number and store it to a file :)
     
Loading...

Share This Page

Loading...