2

我有一个班级,一个电话状态的听众。在此,当此线路的通话断开时,

case TelephonyManager.CALL_STATE_IDLE:
Log.e(TAG, "CALL_STATE_IDLE");
break;

执行不止一次。这严重影响了我的要求。如果有人知道这个问题,请帮助我。

提前致谢

 public class IncomingCallListener extends BroadcastReceiver
        {
            private Context mContext;
            private static final String TAG = "CustomBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            mContext = context;
            TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            CustomPhoneStateListener customPhoneListener = new   CustomPhoneStateListener();
            telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
            Bundle bundle = intent.getExtras();
            String phoneNr= bundle.getString("incoming_number");

        }
        public class CustomPhoneStateListener extends PhoneStateListener
            {
                private static final String TAG = "CustomPhoneStateListener";

                @Override
                public void onCallStateChanged(int state, String incomingNumber) 
                {
                switch (state) 
                {
                    case TelephonyManager.CALL_STATE_RINGING:
                        if(!incomingNumber.equalsIgnoreCase(""))
                        {
                            Log.e(TAG, "CALL_STATE_RINGING");
                        }
                        break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.e(TAG, "CALL_STATE_OFFHOOK");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.e(TAG, "CALL_STATE_IDLE");
                    break;
                default:
                    break;
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        }       
    } 
4

2 回答 2

4

将您的代码更改为:

public class IncomingCallListener extends BroadcastReceiver
        {
            private Context mContext;

            private static final String TAG = "CustomBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            mContext = context;
            Bundle extras = intent.getExtras();
            if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("DEBUG", state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                CustomPhoneStateListener customPhoneListener = new   CustomPhoneStateListener();
                telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
                Bundle bundle = intent.getExtras();
                String phoneNr= bundle.getString("incoming_number");
            }
            else{
                        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                            {
                               // DO SOME CODE HERE...
                            }
                        else{
                        if (state.equals(TelephonyManager.CALL_STATE_OFFHOOK))
                            {
                               // DO SOME CODE HERE...
                            }                          
                        }
            }
        }

     }
//YOUR CODE HERE...
于 2012-11-29T06:40:40.303 回答
0

我改变了一点......这是工作代码......

public class IncomingCallListener extends BroadcastReceiver
    {
        private Context mContext;
        private static final String TAG = "CustomBroadcastReceiver";
         TelephonyManager telephony;
         CustomPhoneStateListener customPhoneListener ;
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            mContext = context;
            Bundle extras = intent.getExtras();
            if (extras != null) {
                String state = extras.getString(TelephonyManager.EXTRA_STATE);
                Log.w("DEBUG", state);

                    telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                    customPhoneListener = new   CustomPhoneStateListener();
                    telephony.listen(customPhoneListener,   PhoneStateListener.LISTEN_CALL_STATE);
                    Bundle bundle = intent.getExtras();
                    String phoneNr= bundle.getString("incoming_number");


            }


        }
        public class CustomPhoneStateListener extends PhoneStateListener
        {
            private static final String TAG = "CustomPhoneStateListener";
            Handler handler=new Handler();
            @Override
            public void onCallStateChanged(int state, String incomingNumber) 
            {
                switch (state) 
                {
                case TelephonyManager.CALL_STATE_RINGING:
                    if(!incomingNumber.equalsIgnoreCase(""))
                    {
                        //YOUR CODE HERE

                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    //YOUR CODE HERE

                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    //YOUR CODE HERE
                    break;
                default:
                    break;
                }
                super.onCallStateChanged(state, incomingNumber);
                telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_NONE);
            }


        }       
    }
于 2012-11-30T04:25:56.973 回答