Trouble with reading phone state

前端 未结 2 1257
误落风尘
误落风尘 2021-01-20 02:43

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 th

相关标签:
2条回答
  • 2021-01-20 03:25

    Have you written the following line :

     telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
    
    0 讨论(0)
  • 2021-01-20 03:32

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

    }

    0 讨论(0)
提交回复
热议问题