GsmCellLocation返回相同的值

时间:2015-04-28 13:07:30

标签: java android location

我试图通过交接捕获细胞ID。 我成功地获得了一个连接到我的手机的手机ID。 但是,我无法解释这一点,似乎没有同步。 我几次漫游,但它总是返回第一个小区id。

public class MainActivity extends Activity {
private TextView textView;
private GsmCellLocation gsmCellLocation;
private TelephonyManager telephonyManager;
private String cId = "", lac = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
    telephonyManager.listen(listener,
            PhoneStateListener.LISTEN_CELL_LOCATION);

}

private Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        switch (msg.what) {
        case 1:
            updateText();
        }
    }

};


public String updateLocation() {
    cId = Integer.toHexString(gsmCellLocation.getCid());
    lac = Integer.toString(gsmCellLocation.getLac());
    return "cId: " + cId + "\nLac: " + lac + "\n===============\n";
}

public void updateText() {
    Toast.makeText(getApplicationContext(), updateLocation(),
            Toast.LENGTH_SHORT).show();
    textView.append(updateLocation());
}

private PhoneStateListener listener = new PhoneStateListener() {

    @Override
    public void onCellLocationChanged(CellLocation location) {
        // TODO Auto-generated method stub
        super.onCellLocationChanged(location);

        handler.sendEmptyMessage(1);
    }

};
}

1 个答案:

答案 0 :(得分:1)

你不应该添加:

MainActivity.this.gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

在onCellLocationChanged方法中?