Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 723
enforcer-99 Avatar asked Feb 21 '12 23:02

enforcer-99


1 Answers

@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);
}
}
like image 133
NaserShaikh Avatar answered Sep 28 '22 20:09

NaserShaikh