我想用手抓取手机号码。我成功地得到了一个手机号码,它一开始连接着我的手机。然而,我不能确切地解释这一点,它似乎不同步。我漫游了好几次,但它总是返回第一个单元格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条答案
按热度按时间ia2d9nvy1#
你不应该加上:
内部oncelllocationchanged方法?