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".
@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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With