enforcer-99
enforcer-99

Reputation: 53

How to determine the phone number of a current caller in a stand-alone application

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".

Upvotes: 5

Views: 2130

Answers (3)

NaserShaikh
NaserShaikh

Reputation: 1576

@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);
}
}

Upvotes: 4

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28717

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.

Upvotes: 1

EGHDK
EGHDK

Reputation: 18130

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?

Upvotes: 3

Related Questions