简体   繁体   English

检测用户是否以编程方式实际拨打了号码

[英]Detect if the user actually dialed a number programmatically

In an app I show some phone numbers in a textview that the user can tap on them to dial them ( autoLink set to phone on the textview). 在一个应用程序,我显示,用户可以在他们身上挖掘拨打他们(一个TextView一些电话号码autoLink设置为phone上的TextView)。
When I tap on the numbers the option to dial that number shows up. 当我点击数字时,会显示拨打该号码的选项。
My question is: Is there a way to figure out if the user actual pressed the dial button in the dial pad to actually do the call? 我的问题是:有没有办法弄清楚用户是否按下拨号盘中的拨号按钮来实际拨打电话?

You can create a GSMBroadcastListener and receive events about the call state of the phone (Hangup, Ringing, Established, etc). 您可以创建GSMBroadcastListener并接收有关电话呼叫状态的事件(Hangup,Ringing,Established等)。

If you want to know, if it happened after a click on your phone button, just create this listener in the on-click event, so it will receive the events only if one of your buttons is clicked. 如果您想知道,如果它在点击您的手机按钮后发生,只需在点击事件中创建此侦听器,这样只有在单击其中一个按钮时它才会收到事件。 Unhook (unregister) in the Hangup-Event after the call has ended, so will not receive events after the call. 在呼叫结束后取消挂起(取消注册)挂断事件,因此在呼叫后不会收到事件。

Here is my implementation with an Listener Interface that works fine in a production app: 这是我的实现与监听器接口在生产应用程序中正常工作:

<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 will forward GSM events to a listener that you add with GSMBroadcastReceiver.registerGSMBroadcastListener . 此类将GSM事件转发给您使用GSMBroadcastReceiver.registerGSMBroadcastListener添加的侦听GSMBroadcastReceiver.registerGSMBroadcastListener

To use it, you need this: 要使用它,您需要:

  • Register the receiver in your manifest manifest注册接收器

     <receiver android:name=".handlers.GSMBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver> 
  • Then, in the click listener of your phone number button, register a listener: (Note, that you unregister it in the onHangUp() callback!) 然后,在您的电话号码按钮的单击侦听器中,注册一个侦听器:(注意,您在onHangUp()回调中取消注册它!)

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

That's it! 而已! You should now get informed about GSM activity after your button click. 按下按钮后,您现在应该了解GSM活动。

Hope this helps, cheers, Gris 希望这有助于,欢呼,格里斯

you can listen the event if dialpad opened or not if dialpad not opened then you can say its from the app..or like the same otherways. 如果拨号盘没有打开,你可以听到事件,如果拨号盘没有打开,那么你可以从应用程序说出来,或者像其他人一样。 or you can call a webservice API with dialing a number from your code. 或者您可以通过拨打代码中的号码来呼叫Web服务API。 Upon the responce you can detect its from your app not actually from dialpad or viceversa. 在响应时,您可以从您的应用程序中检测到它实际上不是来自拨号盘或反之亦然。

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. 它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM