我通过使用以下代码片段和 android BroadCastReceiver 来捕获 android.intent.action.PHONE_STATE 更改事件。我可以成功识别事件并处理来电也可以通过以下代码完成。
我的要求是识别来自特定号码的呼叫,通过代码结束该呼叫并为该传入事件调用 Web 服务。
在这里,此代码针对呼叫接收和结束事件运行两次。
我想停止两次调用此方法。我怎样才能停止为第二个事件调用该代码片段(结束通话)。如果有人能在这方面帮助我,那就太好了。
 if (intent.getAction().equals(
            "android.intent.action.PHONE_STATE")) {
        Log.i("INFO", "Call Received");
        try {
            TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            Bundle bundle = intent.getExtras();
            Log.i("Calling database", "Creatinng DataHandler Object");
            DatabaseHandler db = new DatabaseHandler(context);
            String phoneNumber =db.getContact().getPhoneNumber();
            Log.i("Retriving : ", "Retriving .."+ db.getContact().getPhoneNumber());
            if ((bundle.getString("incoming_number").equals(phoneNumber))) {
                Log.i("Test Call", "This is the test call");
                @SuppressWarnings("rawtypes")
                Class c = Class.forName(telephony.getClass().getName());
                Method m = c.getDeclaredMethod("getITelephony");
                m.setAccessible(true);
                ITelephony telephonyService = (ITelephony) m
                        .invoke(telephony);
                telephonyService = (ITelephony) m.invoke(telephony);
                telephonyService.endCall();
                // Call the web service and send log details
                ServiceClient sc = new ServiceClient();
                sc.serviceCall(context);
            } else {
                Log.i("Normal Call", "This is not the test call");
            }
        } catch (Exception e) {
            Log.i("Exception Occured", "Exception in call code snipet");
        }
    }
}