5

I'd like to build an Android application that can contact the current caller via a pre-determined text message. Sending a text message is simple enough but determining the phone number of the current caller in a stand-alone application is the challenge. Is the there an easy way to divine the phone number so I can send them a message while still on the call?

Of course there are manual ways to do this: write down the number, key it into a new text message, enter the message. But I want to define the message up front and be able to "send it to current caller".

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Please leave some code on what you have actually tried so far. What doesn't work? My examples would seem to do the trick. – EGHDK Jan 12 '13 at 21:47

3 Answers3

4
@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    helper = new ContactDatabaseHelper(context);
    list = helper.getAllContacts();

    try{
        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        if (list.size() != 0){
            for ( int i  = 0, size = list.size(); i < size; i++ ){
                if (PhoneNumberUtils.compare(incomingNumber, list.get(i).getContactNumber())){                  
                    ToastMsg.showToast(context,list.get(i).getContactName()+" Calling");
                }
            }
        }


    }catch (Exception e) {
        // TODO: handle exception
    }   

}


public class PhoneCallStateListener extends PhoneStateListener{
private Context context;

public PhoneCallStateListener(Context context){
    this.context = context;
}

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

    switch (state) {

        case TelephonyManager.CALL_STATE_RINGING:       


            break;
        case PhoneStateListener.LISTEN_CALL_STATE:

    }
    super.onCallStateChanged(state, incomingNumber);
}
}
NaserShaikh
  • 1,576
  • 2
  • 23
  • 39
  • Doesn't this mean the app will need to be loaded all the time in order to intercept these call states? – enforcer-99 Jan 17 '13 at 13:39
  • 2
    @enforcer-99 I don't think you understand the concept of BroadcastReciever. Here is the description from StackOverflow: "BroadcastReceiver is an Android component that responds to system-wide broadcast announcements." and here is the link from google that you should *really* read. Hence... you are responding to a system wide broadcast, and your app is not "loaded all the time". – EGHDK Jan 17 '13 at 23:48
3

For your sistuation the best I can think of is to use PhoneStateListener. It contains onCallStateChanged handler. One of the arguments is a String containing the incoming phone number.

Source: http://developer.android.com/reference/android/telephony/PhoneStateListener.html

Ctrl + F and type in "Incoming" and you will find everything you need to know.

EDIT: To make sure you're app starts on the startup of your phone, just add a BroadcastReciever. How to start an Application on startup?

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • But what about the case where the user does not invoke the app until the phone call is already in progress? Since there will be no state change event - I wouldn't be able to get the number correct? – enforcer-99 Jan 06 '13 at 20:23
  • Sounds like you would need your app to begin on startup. This is similar to what apps like "Google Wallet" do. When I go to a store and tap my phone to pay, I never had to open up Google wallet to have it work. But, when I turn my phone on and look at my notifications I see that "Google Wallet Initializing" really quick and then it goes away. Don't have a lot of time right now, but maybe this will help: http://stackoverflow.com/questions/5290141/android-broadcastreceiver-on-startup – EGHDK Jan 06 '13 at 21:09
  • What if I just checked the Android call log? If I sorted the list by date DESC, would it show the current call I am on or does the current call get pushed to the call log AFTER the call completes? – enforcer-99 Jan 12 '13 at 21:35
  • I'm not sure, but I have a feeling that it would be different on different phones. Some phones with custom apps (like motoblur, or samsung or htc) may work differently. But in all honesty, using a broadcast receiver should work. – EGHDK Jan 12 '13 at 21:43
  • 1
    @enforcer-99 At least on the devices I have, the call log gets updated *after* the call is completed. – Dheeraj Vepakomma Jan 13 '13 at 05:41
  • 1
    Confirmed - checking the call log for the latest caller during a call results in the previous caller - not the current. – enforcer-99 Jan 17 '13 at 13:26
  • 1
    Agreed - it looks I might just have to start the app on boot and hope the user doesn't kill it using some battery-saver app. I hate that though. It seems like such as simple request to ask the phone "Who is the current caller?" I'm personally not a fan of apps running all the time in the background. – enforcer-99 Jan 19 '13 at 00:38
1

Register a BroadcastReceiver in your manifest that listens to ACTION_PHONE_STATE_CHANGED.

Broadcast intent action indicating that the call state (cellular) on the device has changed.

The EXTRA_STATE extra indicates the new call state. If the new state is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming phone number as a String.

Requires the READ_PHONE_STATE permission.

This was a sticky broadcast in version 1.0, but it is no longer sticky. Instead, use getCallState() to synchronously query the current call state.

This way you don't need the user to launch your app before receiving a call.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • What about this? : "As of Android 3.1 the Android system will by default exclude all BroadcastReceiver from receiving Intents if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu (in Manage Application). This is an additional security features as the user can be sure that only the applications he started will receive broadcast Intents." – enforcer-99 Jan 19 '13 at 00:34