Vikalp Patel
Vikalp Patel

Reputation: 10887

PhoneStateListener for call receiver not working in real device

I've created BroadCastReceiver which listens for IncomingCall and Outgoing Calls. Tried to add PhoneStateListener with TelephonyManager in BroadCastReceiver which works perfectly on Emulator and some devices.

It's not working with Samsung ST 7652. Always return CALL_STATE_IDLE and incoming number null .Why is it so?

As below :

public class ReceiverInComingCall extends BroadcastReceiver { 
   public void onReceive(Context context, Intent intent) {
    TelephonyManager phone = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
    phone.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
   }

  private class MyPhoneStateListener extends PhoneStateListener{
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch(state){
        case TelephonyManager.CALL_STATE_RINGING:
         Log.i("CALL","CALL_STATE_RINGING");
        break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
         Log.i("CALL","CALL_STATE_OFFHOOK");
        break;
        case TelephonyManager.CALL_STATE_IDLE:
         Log.i("CALL","CALL_STATE_IDLE");
        break;
        }
      }
   }
 }

Bugging me out :-

  E/MSimTelephonyManager(621): MSimTelephonyManager sRegistryMsim != null

Why does it giving something like these in LogCat when i debugged for Samsung ST 7652 else it show me perfect Log of Call State?

Tried

PhoneStateListener not working with some devices. But it didn't help me with that solution either Always giving me CALL_STATE_IDLE and incoming number null.

Upvotes: 1

Views: 3936

Answers (1)

Vikalp Patel
Vikalp Patel

Reputation: 10887

After browsing several source regarding PhoneStateListeners and android.intent.action.PHONE_STATE found what I was looking for and works like a charm regardless of model specific barriers.

BroadCastReceiver

 public class PhoneStateReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String str = intent.getAction();
    if ("android.intent.action.PHONE_STATE".equals(str))
        inComing(context, intent);

    if ("android.intent.action.NEW_OUTGOING_CALL".equals(str))
        outGoing(context, intent);
  }

 private void inComing(Context context, Intent intent){
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
        Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
        Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
        Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }

 private void trueCallerOutgoing(Context context, Intent intent)
  {
    String callState = intent.getStringExtra("state");
    if ("RINGING".equals(callState)){
            Log.i(TAG, "RINGING SENDS BUSY");
    }else if ("OFFHOOK".equals(callState)){
            Log.i(TAG, "OFFHOOK SENDS BUSY");
    }else if("IDLE".equals(callState)){
            Log.i(TAG, "IDLE SENDS AVAILABLE"); 
    }
  }
}

Manifest

 <receiver android:name="PhoneStateReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

With Permission

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Upvotes: 3

Related Questions