onCellLocationChanged的问题

时间:2014-05-15 11:17:54

标签: android telephonymanager phone-state-listener

我需要在设备更改单元格连接时启动服务,并且我假设使用下面的onCellLocationChanged()工具执行此操作,但只有在收到来电时才会启动蜂鸣声...

我的听众:

public class CellLocationListener extends PhoneStateListener {

    @Override
    public void onCellLocationChanged(CellLocation location) {
        super.onCellLocationChanged(location);

        int cid = 0;
        int lac = 0;

        if (location != null) {
            if (location instanceof GsmCellLocation) {
                cid = ((GsmCellLocation) location).getCid();
                lac = ((GsmCellLocation) location).getLac();
            }
            else if (location instanceof CdmaCellLocation) {
                cid = ((CdmaCellLocation) location).getBaseStationId();
                lac = ((CdmaCellLocation) location).getSystemId();
            }
        }

        String cellInfo = Integer.toString(lac)+"-"+Integer.toString(cid);

        Log.v("logg","CELL CHANGED:"+cellInfo);
    }
}

我在mainactivity上使用了相同的cellInfo,当我启动它时,cellInfo会查找其值,但没有来自广播接收器的蜂鸣声....

清单上的

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

<receiver android:name="dado.auto3gdataswitch.CellChangeReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

我的broadcastReceiver

  public class CellChangeReceiver extends BroadcastReceiver {

     TelephonyManager telephony;

     public void onReceive(Context context, Intent intent) {
        CellLocationListener phoneListener = new CellLocationListener();
        telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, CellLocationListener.LISTEN_CALL_STATE);

        Toast.makeText(context, "ON receiver", Toast.LENGTH_LONG).show();

        final MediaPlayer mp = MediaPlayer.create(context, R.raw.beep); 
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {      
                mp.stop();
                mp.release();
            }
        });

        Log.v("logg","onreceiver");

    }
}

只有在接到来电时我才会听到嘟嘟声,如果我换了电话就听不到......

出了什么问题?

2 个答案:

答案 0 :(得分:4)

您正在聆听CellLocationListener.LISTEN_CALL_STATE,但您应该聆听CellLocationListener.LISTEN_CELL_LOCATION。如果您希望在单元格更改时播放声音,则应将相应的代码移动到onCellLocationChanged。只有在收到android.intent.action.PHONE_STATE时才会注册您的小区更改侦听器。

答案 1 :(得分:0)

找到了更好的解决方案!!!

在我的mainActivity中:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TelephonyManager telm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    int events = PhoneStateListener.LISTEN_CELL_LOCATION;
    telm.listen(phoneStateListener, events);

.....
}

private final PhoneStateListener phoneStateListener = new PhoneStateListener() {

    @Override
    public void onCellLocationChanged(CellLocation location) {
        super.onCellLocationChanged(location);

        int cid = 0;
        int lac = 0;

        if (location != null) {
            if (location instanceof GsmCellLocation) {
                cid = ((GsmCellLocation) location).getCid();
                lac = ((GsmCellLocation) location).getLac();
            }
            else if (location instanceof CdmaCellLocation) {
                cid = ((CdmaCellLocation) location).getBaseStationId();
                lac = ((CdmaCellLocation) location).getSystemId();
            }
        }

        String cellBase = Integer.toString(lac)+"-"+Integer.toString(cid);

        Toast.makeText(getBaseContext(), cellBase, Toast.LENGTH_LONG).show();
        Log.v("logg", "cell:"+cellBase);

        final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.beep);    
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

                mp.stop();
                mp.release();
            }
        });
    }       
};