在服务内的线程中调用 PhoneStateListener

Calling PhoneStateListener in a thread inside a service

我正在开发一款音乐应用,该应用使用一项服务在后台播放歌曲。在我的服务中,我有一个代码,用于监听 phone 状态并在有来电时暂停音乐,然后在通话结束后再次播放。

此代码在 onCreate 中运行良好,但当我将其放入 Thread 中时,它给出: java.lang.NullPointerException:尝试从字段 'android.os.MessageQueue android.os.Looper.mQueue' 读取空对象引用

我试过的:我试过在代码中放一个Looper.prepare。这防止了它崩溃,但是当 phone 来电时,歌曲不会停止。估计是我没用对Looper功能

final class phoneStateThread implements Runnable
    {

        @Override
        public void run() {


                Log.v(TAG,"Starting telephony");
                telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                Log.v(TAG,"Starting listener");
                phoneStateListener = new PhoneStateListener(){
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        Log.v(TAG,"Starting CallStateChange");
                        switch (state){

                            //=====Phone ringing. Pause music=====
                            case TelephonyManager.CALL_STATE_OFFHOOK:
                            case TelephonyManager.CALL_STATE_RINGING:
                                if(mediaPlayer!=null) {
                                    pauseMedia();
                                    isPausedInCall=true;
                                }

                                break;

                            //=====Phone idle. start music=====
                            case TelephonyManager.CALL_STATE_IDLE:
                                if(mediaPlayer!=null){
                                    if(isPausedInCall){
                                        isPausedInCall=false;
                                        playMedia();
                                    }
                                }
                                break;
                        }

                    }

                };


                telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
            }
        }

This code works fine inside onCreate

因为使用了Android Main Thread的Looper,当你把代码放到Thread里面的时候,Looper那里没有,你需要自己制作Looper。试试这个

final class phoneStateThread implements Runnable
    {

        @Override
        public void run() {

                Looper.prepare()
                Log.v(TAG,"Starting telephony");
                telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                Log.v(TAG,"Starting listener");
                phoneStateListener = new PhoneStateListener(){
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        Log.v(TAG,"Starting CallStateChange");
                        switch (state){

                            //=====Phone ringing. Pause music=====
                            case TelephonyManager.CALL_STATE_OFFHOOK:
                            case TelephonyManager.CALL_STATE_RINGING:
                                if(mediaPlayer!=null) {
                                    pauseMedia();
                                    isPausedInCall=true;
                                }

                                break;

                            //=====Phone idle. start music=====
                            case TelephonyManager.CALL_STATE_IDLE:
                                if(mediaPlayer!=null){
                                    if(isPausedInCall){
                                        isPausedInCall=false;
                                        playMedia();
                                    }
                                }
                                break;
                        }

                    }

                };


                telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
                Looper.loop()
            }
        }