Question

I want to perform some operation (Pause game) in my application when a call came. But reading the phone state is not working. I have given permission(READ_PHONE_STATE) in the manifest. Nothing is happen when a call came. Thanks.

TelephonyManager telephonyManager;
PhoneStateListener listener;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
*
*
*
listener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Toast.makeText(SudokuGameActivity.this, "IDLE", Toast.LENGTH_SHORT).show();
          break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
         Toast.makeText(SudokuGameActivity.this, "OFF Hook", Toast.LENGTH_SHORT).show();
          break;
        case TelephonyManager.CALL_STATE_RINGING:
            Toast.makeText(SudokuGameActivity.this, "Ringing", Toast.LENGTH_SHORT).show();
            mpauseButton.performClick();
          break;
          }

        }
       };
Was it helpful?

Solution

Have you written the following line :

 telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);

OTHER TIPS

when your listener has be created, you need invoke `public void listen (PhoneStateListener listener, int events)' to listen.

also, you can try this: create a broadcatst receiver handle the action android.intent.action.PHONE_STATE,

code example:

public class PhoneStateReceiver extends BroadcastReceiver {

private TelephonyManager manager;

@Override
public void onReceive(Context context, Intent intent) {
    if (manager == null) {
        manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    String action = intent.getAction();
    System.out.println(action);
    System.out.println("current phone state:" + manager.getCallState());
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top