Android Java获取基站信息出现Unexpected Identifier语法错误求助
解决Termux中ecj编译Android基站代码的语法错误问题
我帮你排查了下,你遇到的语法错误主要来自三个方面,都是从网页复制代码时容易踩的典型坑:
1. HTML转义字符导致的语法错误
你代码里的"(对应双引号")、<(对应小于号<)都是网页的HTML实体字符,Java编译器完全不认识这些,必须替换成对应的原生字符:
- 把所有
"替换为" - 把所有
<替换为<
比如第一段里的这段代码:
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
要改成:
phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType;
第二段里的List<NeighboringCellInfo>要改成List<NeighboringCellInfo>,Log.e("xxxxx", "Size: " + neighborInfo.size() );改成Log.e("xxxxx", "Size: " + neighborInfo.size() );
2. 代码缺少类和方法的包裹(核心语法问题)
Java是纯面向对象语言,所有可执行代码必须放在类的方法内部,你提供的两段代码都不符合这个基础规则:
第一段代码的修正
它只是一个静态方法getCellInfo,但没有被包裹在类里。你需要给它套一个类的外壳,同时补充必要的import语句(否则编译会报找不到类的错误):
import android.content.Context; import android.telephony.TelephonyManager; import android.telephony.CellInfo; import android.telephony.CellInfoGsm; import android.telephony.CellInfoLte; import android.telephony.CellSignalStrengthGsm; import android.telephony.CellSignalStrengthLte; import android.telephony.CellIdentityGsm; import android.telephony.CellIdentityLte; import android.telephony.NeighboringCellInfo; import android.os.Build; import org.json.JSONArray; import org.json.JSONObject; import java.util.List; public class CellInfoUtils { public static JSONArray getCellInfo(Context ctx){ TelephonyManager tel = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); JSONArray cellList = new JSONArray(); // Type of the network int phoneTypeInt = tel.getPhoneType(); String phoneType = null; phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_GSM ? "gsm" : phoneType; phoneType = phoneTypeInt == TelephonyManager.PHONE_TYPE_CDMA ? "cdma" : phoneType; //from Android M up must use getAllCellInfo if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { List<NeighboringCellInfo> neighCells = tel.getNeighboringCellInfo(); for (int i = 0; i < neighCells.size(); i++) { try { JSONObject cellObj = new JSONObject(); NeighboringCellInfo thisCell = neighCells.get(i); cellObj.put("cellId", thisCell.getCid()); cellObj.put("lac", thisCell.getLac()); cellObj.put("rssi", thisCell.getRssi()); cellList.put(cellObj); } catch (Exception e) { } } } else { List<CellInfo> infos = tel.getAllCellInfo(); for (int i = 0; i<infos.size(); ++i) { try { JSONObject cellObj = new JSONObject(); CellInfo info = infos.get(i); if (info instanceof CellInfoGsm){ CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength(); CellIdentityGsm identityGsm = ((CellInfoGsm) info).getCellIdentity(); cellObj.put("cellId", identityGsm.getCid()); cellObj.put("lac", identityGsm.getLac()); cellObj.put("dbm", gsm.getDbm()); cellList.put(cellObj); } else if (info instanceof CellInfoLte) { CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength(); CellIdentityLte identityLte = ((CellInfoLte) info).getCellIdentity(); cellObj.put("cellId", identityLte.getCi()); cellObj.put("tac", identityLte.getTac()); cellObj.put("dbm", lte.getDbm()); cellList.put(cellObj); } } catch (Exception ex) { } } } return cellList; } }
第二段代码的修正
它直接写了执行语句,没有类和方法包裹,而且最后还多了无关的“的内容”字符,需要改成类似这样的结构:
import android.content.Context; import android.telephony.TelephonyManager; import android.telephony.NeighboringCellInfo; import android.util.Log; import java.util.List; public class NeighborCellChecker { public void checkNeighborCells(Context context) { TelephonyManager teleManager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); List<NeighboringCellInfo> neighborInfo = teleManager.getNeighboringCellInfo(); Log.e("xxxxx", "Size: " + neighborInfo.size() ); } }
这里用Context参数来获取ApplicationContext,避免直接在非Activity类里调用getApplicationContext()的问题。
3. Termux中ecj编译的额外注意事项
- 确保你已经安装了ecj:
pkg install ecj - 编译时必须指定Android的
android.jar到classpath(否则会找不到Android相关类),比如:ecj -cp /path/to/android.jar CellInfoUtils.java - 注意API兼容性:
getNeighboringCellInfo()在Android O及以上已被废弃,运行时可能返回null,实际使用时要做判空处理。
内容的提问来源于stack exchange,提问作者Mario De Lorenzo
相关产品推荐
相关产品推荐

