GsmCellLocation returns 相同的值
GsmCellLocation returns a same value
我正在尝试通过交接来获取小区 ID。
我成功地获得了一个刚开始连接到我手机的cell id。
不过这个我也说不清楚,好像不同步。
我漫游了好几次,但它总是 returns 第一个小区 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);
}
};
}
你不应该添加:
MainActivity.this.gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
在 onCellLocationChanged 方法中?
我正在尝试通过交接来获取小区 ID。 我成功地获得了一个刚开始连接到我手机的cell id。 不过这个我也说不清楚,好像不同步。 我漫游了好几次,但它总是 returns 第一个小区 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);
}
};
}
你不应该添加:
MainActivity.this.gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
在 onCellLocationChanged 方法中?