簡體   English   中英

如何從 Android PhoneStateListener 返回值?

[英]How to return value from Android PhoneStateListener?

我想獲取當前用於數據傳輸的單元格信息。 在 Android Q 之前,我可以通過getallcellinfo()輕松獲取它。 但是,從 Android Q 開始,這將通過onCellInfoChanged()報告。

我不需要監聽單元格信息的變化,我只需要調用該方法時的單元格信息。 我不能只返回((CellInfoLte) cellInfo).getCellIdentity().getEarfcn(); 在 PhoneStateListener 中。 如何實施?

public int getEarfcn() {
  telephonyManager.listen(new PhoneStateListener() {
    @Override
    public void onCellInfoChanged(List<CellInfo> cellInfoList) {
      super.onCellInfoChanged(cellInfoList);
      for (CellInfo cellInfo : cellInfoList) {
        if (cellInfo instanceof CellInfoLte) {
          int earfcn = ((CellInfoLte) cellInfo).getCellIdentity().getEarfcn();
          // How to return this "earfcn" once I get it?
        }
      }
    }
  }, PhoneStateListener.LISTEN_CALL_STATE);
  return CellInfo.UNAVAILABLE;
}

您不能使用退貨。 創建一個相同的界面

public voi getEarfcn(ICellInfoChanged cellInfoChangedCallback) {
  telephonyManager.listen(new PhoneStateListener() {
    @Override
    public void onCellInfoChanged(List<CellInfo> cellInfoList) {
      super.onCellInfoChanged(cellInfoList);
      for (CellInfo cellInfo : cellInfoList) {
        if (cellInfo instanceof CellInfoLte) {
          int earfcn = ((CellInfoLte) cellInfo).getCellIdentity().getEarfcn();
          cellInfoChangedCallback.onCallback(cellInfo);
        }
      }
    }
  }, PhoneStateListener.LISTEN_CALL_STATE);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM