Bana
Bana

Reputation: 162

GsmCellLocation returns a same value

I'm trying to catch cell ids by hand-off. I succeeded to get a cell id that was connecting to my cellphone at first. However, I can't explain this exactly, it seems not to synchronize. I roamed around several times but, it always returns the first cell 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);
    }

};
}

Upvotes: 0

Views: 250

Answers (1)

hkN
hkN

Reputation: 153

Shouldn't you add :

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

inside onCellLocationChanged method ?

Upvotes: 1

Related Questions