尝试使用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);
由于在documentation所述,LISTEN_CELL_LOCATION要求您添加以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
这是错误的。 @ nkout的答案是正确的答案。 – 2015-04-16 23:20:09