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

I would like to create an Android app that can contact the current caller using a predefined text message. Sending a text message is quite simple, but determining the phone number of the current caller in a stand-alone application is a call. Is there an easy way to guess the phone number so that I can send them a message while he is still calling?

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

+5
source share
3 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);
}
}
+4

PhoneStateListener. onCallStateChanged. , .

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

Ctrl + F "", , .

: , , BroadcastReciever. ?

+3

BroadcastReceiver , ACTION_PHONE_STATE_CHANGED.

A broadcast translation action indicating that the call status (cellular) of the device has changed.

The EXTRA_STATEextra screen indicates the new call status. If the new state RINGING, the second additional EXTRA_INCOMING_NUMBERprovides the incoming phone number as a string.

Permission required READ_PHONE_STATE.

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

Thus, the user does not need to run the application before accepting the call.

+1
source

All Articles