Frage

my broadcast is getting called but PhoneStateListener is not called my code works fine when I run it in emulator but when I try it on actual device PhoneStateListener never gets a call, I am going nutts on 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 onReceive() method and here is my class extensing PhoneStateListener outside onReceive() but in 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 it will be accepted Thanks in advance

War es hilfreich?

Lösung

Your service is getting cleaned up before the callback gets invoked. You should not really be relying on anything allocated in your BroadcastReceiver to exist after onReceive exits. You should put CustomPhoneStateListener in Service or Activity. Then you can use an Intent to launch the activity or service to do your state monitoring.

From the BroadcastReceiver docs,

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

The reason it is working on the emulator is probably because there are less processes in general on the emulator and for some reason it seems less aggressive about killing off processes.

Andere Tipps

Tthe solution for above problem is:

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

    }  

}; 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top