Android的广播接收器和android.intent.action.PHONE_STATE事件 [英] Android BroadCastReceiver and android.intent.action.PHONE_STATE Event

查看:1276
本文介绍了Android的广播接收器和android.intent.action.PHONE_STATE事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的Andr​​oid的BroadcastReceiver code片段捕捉android.intent.action.PHONE_STATE更改事件。我可以成功地承认事件,并处理来电可以按照code来完成。

I am catching the android.intent.action.PHONE_STATE change event by using following code snippet with android BroadCastReceiver. I could recognize the event successfully and handle the incoming calls also can be done by following code.

我的要求是确定哪些是来形成特定数量的通过code打电话调用Web服务传入事件调用,结束。

My requirement is identifying calls which are coming form a particular number, end that call through the code and invoke a web service for that incoming event.

在这里这个code呼叫接听和结束的事件运行两次。

In here this code runs twice for call receiving and ending events.

我要停止调用两次这个方法。我怎样才能停止调用第二个事件(结束通话)的code片段。这将是巨大的,充满如果任何人能帮助我在此。

I want to stop calling this method for twice. How can i stop calling that code snippet for the second event (ending the call). It would be great-full if any one can help me on this.

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

}

推荐答案

我相信您遇到的问题是,手机状态变化了两次,一次是当电话铃声响起,然后,结束通话后,当手机进入处于闲置状态。

I believe the issue you are having is that the phone state changes twice, once when the phone rings, then, after you end the call, when the phone goes idle.

您只希望这样做,最好的办法是增加手机状态的其他检查,看它是否振铃,如果电话响起,然后挂断电话这code运行一次。

You only want this code to run once so the best way to do that is to add an additional check of the phone state to see if it is ringing, if the phone is ringing then end the call.

这可通过添加以下检查来完成:

This can be accomplished by adding the following the check:

if (intent.getAction().equals(
        "android.intent.action.PHONE_STATE")) {
    Log.i("INFO", "Call Received");

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//your code here that starts with try block
}

希望帮助

这篇关于Android的广播接收器和android.intent.action.PHONE_STATE事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆