尝试使用 PhoneStateListener,如下所示:
首先,创建监听器。
public PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCellLocationChanged (CellLocation location) {
StringBuffer str = new StringBuffer();
// GSM
if (location instanceof GsmCellLocation) {
GsmCellLocation loc = (GsmCellLocation) location;
str.append("gsm ");
str.append(loc.getCid());
str.append(" ");
str.append(loc.getLac());
Log.d(TAG, str.toString());
}
}
};
然后在 onCreate() 上注册监听器,如下所示:
telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
如文档所述,LISTEN_CELL_LOCATION 要求您添加以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>