简体   繁体   中英

Why do I have so many LogCat messages while receive call?

I'm trying to detect when user receive call,call to somebody, when call ended. Can somebody explain me why do I have so many Log messages while receiving a phone call and calling?

My receiver in Android Manifest

<receiver android:name="com.prjct3.amadey.myapplication3.MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
                </intent-filter>
            </receiver>

This is my code.

public class MyReceiver extends BroadcastReceiver {
    PhoneStateListener listener;
    String incoming_nr;
    private int prev_state;
    String TAG="Point_1";
    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.i("Point","onReceive");
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        listener=new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                // TODO Auto-generated method stub

                super.onCallStateChanged(state, incomingNumber);
                 switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "CALL_STATE_RINGING");
                        prev_state=state;
                        break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d(TAG, "CALL_STATE_OFFHOOK");
                prev_state=state;
                break;
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.d(TAG, "CALL_STATE_IDLE==>"+incomingNumber);
                    if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){
                        prev_state=state;
                        //Answered Call which is ended
                    }
                    if((prev_state==TelephonyManager.CALL_STATE_RINGING)){
                        prev_state=state;
                        //Rejected or Missed call
                    }
                    break;

            }
            }

        };
        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

And my LogCat

06-19 11:13:26.580    7519-7519/? D/Point_1﹕ CALL_STATE_IDLE==>null
06-19 11:13:26.705    7519-7519/? D/Point_1﹕ CALL_STATE_IDLE==>null
06-19 11:13:26.706    7519-7519/? D/Point_1﹕ CALL_STATE_IDLE==>null
06-19 11:19:05.669    7704-7704/? D/Point_1﹕ CALL_STATE_RINGING
06-19 11:19:05.669    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:08.471    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>+76398291345
06-19 11:19:08.523    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>+76398291345
06-19 11:19:08.524    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:32.053    7704-7704/? D/Point_1﹕ CALL_STATE_OFFHOOK
06-19 11:19:32.053    7704-7704/? D/Point_1﹕ CALL_STATE_OFFHOOK
06-19 11:19:32.076    7704-7704/? D/Point_1﹕ CALL_STATE_OFFHOOK
06-19 11:19:32.076    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:39.248    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:39.271    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:39.288    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:39.333    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>
06-19 11:19:39.333    7704-7704/? D/Point_1﹕ CALL_STATE_IDLE==>

Thanks.

when ever your broadcast receiver's onReceive() was invoked ,every time you are trying to listen a new PhoneStateListener.You just need to do it once.

if (phoneStateListener == null) {
phoneStateListener = new ThePhoneStateListener(arg0);
}

check the condition whether phone state listener is null or not then initialize it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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