Detect if user actually dialed a number programmatically

In the app, I am showing some phone numbers in text view that the user can click on to dial them ( autoLink

set to phone

in text view).
When I click on the numbers, it becomes possible to dial the number.
My question is, is there a way to find out if the user actually pressed the dial button on the dial pad to actually call?

+3


source to share


3 answers


You can create a GSMBroadcastListener and receive events about the phone call state (Hangup, Ringing, Foundlished, etc.).

If you want to know if this happened after the phone button was pressed, just create this listener on the on-click event so that it only receives events when one of your buttons is pressed. Unhook (unregister) in the Hangup event after the call ends, so don't get any post-call events.

Here is my implementation with a Listener interface that works great in a production application:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>

      

code:

public interface GSMBroadcastListener {
    void onHangUp(
    void onEstablished();
    void onRinging();
}


public class GSMBroadcastReceiver extends BroadcastReceiver {

    private static GSMBroadcastListener handler = null;
    private static PrivateListener privateListener = null;

    public static void registerGSMBroadcastListener(@Nullable GSMBroadcastListener listener) {
        handler = listener;
    }

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

        if (privateListener == null) {
            privateListener = new PrivateListener();
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            telephonyManager.listen(privateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }

    }

    private class PrivateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);

            Log.i("PHONE_STATE", String.format("GSM event \"onCallStateChanged\" received: state=%d; incomingNumber=\"%s\";", state, incomingNumber));
            if (handler != null) {
                if (state == TelephonyManager.CALL_STATE_IDLE) {
                    Log.i("PHONE_STATE", "Forwarding event as \"GSM onHangUp\".");
                    handler.onHangUp();
                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    Log.i("PHONE_STATE", "Forwarding event as \"GSM onEstablished\".");
                    handler.onEstablished();
                } else if (state == TelephonyManager.CALL_STATE_RINGING) {
                    Log.i("PHONE_STATE", "Forwarding event as \"GSM onRinging\".");
                    handler.onRinging();
                }
            }
        }
    }

}

      



This class forwards GSM events to the listener you add with GSMBroadcastReceiver.registerGSMBroadcastListener

.

To use it, you need the following:

  • Register the receiver with manifest

    <receiver android:name=".handlers.GSMBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>
    
          

  • Then, by clicking the listen button of your phone number, register the listener: (Note that you will unregister in the onHangUp () callback!)

    GSMBroadcastReceiver.registerGSMBroadcastListener(new GSMBroadcastListener() {
    @Override
    public void onRinging() {
        Log.i("GSM", "GSM event \"onRinging\" received.");
    }
    
    @Override
    public void onEstablished() {
        Log.i("GSM", "GSM event \"onEstablished\" received.");
    }
    
    @Override
    public void onHangUp() {
        Log.i("GSM", "GSM event \"onHangUp\" received.");
        GSMBroadcastReceiver.registerGSMBroadcastListener(null); // Unregister the handler!
    }
    });
    
          

What is it! Now you should get information about GSM activity after pressing the button.

Hope this helps, amuses, Gris

+1


source


you can listen for event if dialpad is open or not, if dialpad is not open then you can tell it from application ... and like everyone else. or you can call the dialed API web service from your code. After answering, you might find it from your application not actually from dialpada or vice versa.



0


source


public class Outgoing_Call_Receiver extends BroadcastReceiver {


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


        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            if(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER").equals(new MyApp(context).getEmergencyNumber()));

        }
    }

}

      

Try this code. it will help you.

0


source







All Articles