PhoneStateListener never gets called in android broadcast

my broadcast is called, but the PhoneStateListener is not called, my code works fine when i run it in the emulator, but when i try to use it on the device itself, the PhoneStateListener never gets a call, i'm going to use this problem Here is my code:

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object  
                            CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();  
                            telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager 

      

the above code is in the onReceive () method and here is my extension PhoneStateListener class outside of onReceive () but in the broadcast class.

public class CustomPhoneStateListener extends PhoneStateListener {  

        private static final String TAG = "CustomPhoneStateListener";  



        @Override  
        public void onCallStateChanged(int state, String phonenumber){  

            if(phonenumber!=null && phonenumber.length()>0) 
                incoming_nr=phonenumber;   
            act=new Call_RecorderActivity();

            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:  
                     prev_state=state;
                     Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);



                    break;  
                   // Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
                   // |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

            } //switch close
        }
}

      

logcat shows this on incoming call

01-22 11:25:12.529: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=168]
01-22 11:25:12.539: I/IncomingCallReceiver(1463): State: RINGING
01-22 11:25:12.539: I/IncomingCallReceiver(1463): Incomng Number: +9184848xxxx2
01-22 11:25:12.779: D/CustomPhoneStateListener(1463): CALL_STATE_IDLE==>+9184848xxxx2
01-22 11:25:16.299: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=92]
01-22 11:25:16.299: I/IncomingCallReceiver(1463): State: OFFHOOK
01-22 11:25:18.849: I/IncomingCallReceiver(1463): Bundle[mParcelledData.dataSize=88]
01-22 11:25:18.849: I/IncomingCallReceiver(1463): State: IDLE

      

Any suggestion related to this would be accepted Thanks in advance

+3


source to share


2 answers


Your service is cleared before calling the callback. You don't have to rely on everything that was allocated in BroadcastReceiver

order to exist after the release onReceive

. You have to put CustomPhoneStateListener

in Service

or Activity

. Then you can use Intent

to start an activity or service to monitor the status.

In documents BroadcastReceiver



Once you return from onReceive (), the BroadcastReceiver will no longer be active, and its hosting process is just as important as any other that runs in it. This is especially important because if there is a BroadcastReceiver in this process (a common case for applications that the user has never or recently interacted with), then upon return from onReceive (), the system will consider its process empty and aggressively kill it so that resources are available for other important processes.

The reason he is working on an emulator is probably because there are no processes in the emulator at all, and for some reason it is less aggressive about killing processes.

+2


source


The solution to this problem:



private final PhoneStateListener phoneStateListener = new PhoneStateListener() {  

    @Override 

    public void onCallStateChanged(int state, String incomingNumber) {  

        String callState = "UNKNOWN";  

        String myNumber = tm.getLine1Number();
        switch (state) {  

        case TelephonyManager.CALL_STATE_IDLE:  

            callState = "IDLE";
            if(Status!=""){
                Toast.makeText(mContext,"Call Ends " + incomingNumber,Toast.LENGTH_LONG).show();
            }
            break;  

        case TelephonyManager.CALL_STATE_RINGING: 
            Status = "RINGING";
            if (incomingNumber.startsWith("00")) {  
                Toast.makeText(mContext,"International Call- " + incomingNumber,Toast.LENGTH_LONG).show();  
                callState = "International - Ringing (" + incomingNumber+ ")";  
            } else {  
                Toast.makeText(mContext, "Local Call - " + incomingNumber, Toast.LENGTH_LONG).show();  
                callState = "Local - Ringing (" + incomingNumber + ")";  
            }  
            break;  
        case TelephonyManager.CALL_STATE_OFFHOOK:  

            try{
                String dialingNumber = mIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);  
                if(dialingNumber==null){
                    Status = "Recieve";
                    Toast.makeText(mContext, "Recieve call", Toast.LENGTH_LONG).show();
                }else{
                    Status = "Dialing";
                    if (dialingNumber.startsWith("00")) {  
                        Toast.makeText(mContext,"International - " + dialingNumber,Toast.LENGTH_LONG).show();  
                        callState = "International - Dialing (" + dialingNumber+ ")";  
                    } else {  
                        Toast.makeText(mContext, "Local Call - " + dialingNumber,Toast.LENGTH_LONG).show();  
                        callState = "Local - Dialing (" + dialingNumber + ")";  
                    }  
                }
            }catch(Exception e){}

            break;  

        }  

        Log.i(">>>Broadcast", "onCallStateChanged " + callState);  

        super.onCallStateChanged(state, incomingNumber);  

    }  

}; 

      

0


source







All Articles