gsmcelllocation返回相同的值

7dl7o3gd  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(195)

我想用手抓取手机号码。我成功地得到了一个手机号码,它一开始连接着我的手机。然而,我不能确切地解释这一点,它似乎不同步。我漫游了好几次,但它总是返回第一个单元格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);
    }

};
}
ia2d9nvy

ia2d9nvy1#

你不应该加上:

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

内部oncelllocationchanged方法?

相关问题