PhoneStateListener表现得很奇怪?

时间:2014-09-10 10:23:10

标签: android

发生的奇怪事情就是当我听到所有三个州时

  1. LISTEN_CELL_INFO

  2. LISTEN_CELL_LOCATION

  3. LISTEN_SIGNAL_STRENGTHS

  4. oncellInfoChanged()onCellLocationChanged()仅在注册时被调用一次,我只会定期更新onSignalStrengthsChanged()。但是,如果我评论LISTEN_SIGNAL_STRENGTHS,其他两个开始正确获取更新。任何人都可以指出这个问题吗?

    public class GSMCellInfoListener {
    
            TelephonyManager teleManager = null;
            Context mContext;
            GSMCellInfoProvider mGsmCellInfoProvider;
    
            public GSMCellInfoListener(Context ctx) {
                mContext = ctx;
                mGsmCellInfoProvider = new GSMCellInfoProvider();
            }
    
            public void addListener() {
                teleManager = (TelephonyManager) mContext
                        .getSystemService(Context.TELEPHONY_SERVICE);
                teleManager.listen(mGsmCellInfoProvider,
                        PhoneStateListener.LISTEN_CELL_INFO);
                teleManager.listen(mGsmCellInfoProvider,
                        PhoneStateListener.LISTEN_CELL_LOCATION);
                teleManager.listen(mGsmCellInfoProvider,
                        PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            }
    
            public void removeListener() {
                teleManager
                        .listen(mGsmCellInfoProvider, PhoneStateListener.LISTEN_NONE);
            }
    
            public void deleteListener() {
                teleManager = null;
                mGsmCellInfoProvider = null;
            }
    
            class GSMCellInfoProvider extends PhoneStateListener {
                @Override
                public void onCellInfoChanged(List<CellInfo> cellInfo) {
                    super.onCellInfoChanged(cellInfo);
                }
    
                @Override
                public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                    super.onSignalStrengthsChanged(signalStrength);
                }
    
                @Override
                public void onCellLocationChanged(CellLocation location) {
                    super.onCellLocationChanged(location);
                }
            }
        }
    

1 个答案:

答案 0 :(得分:1)

您是否曾尝试仅使用一名听众同时注册所有3个活动?在内部,mGsmCellInfoProvider可能位于Map或Set中,因此不允许重复,导致后来的.listen(...)调用覆盖之前的。(/ p>

试试这个:

public void addListener() {
        teleManager = (TelephonyManager) mContext
                .getSystemService(Context.TELEPHONY_SERVICE);
        int listenMask = PhoneStateListener.LISTEN_CELL_INFO +
                         PhoneStateListener.LISTEN_CELL_LOCATION +
                         PhoneStateListener.LISTEN_SIGNAL_STRENGTHS;
        teleManager.listen(mGsmCellInfoProvider,listenMask);
    }