在PhoneStateListener上侦听delphi firemonkey中的OnCellInfoChanged的问题

时间:2019-03-08 06:08:03

标签: android delphi firemonkey telephonymanager phone-state-listener


如果能帮助我,将不胜感激。我正在用delphi firemonkey编写一个显示移动单元信息的程序。我需要使用JPhoneStateListener类来监听单元格信息的更改。
首先,我为实现主类编写一个类,如下所示:

    type
    TPL = class(TJavaGenericImport<JPhoneStateListenerClass, JPhoneStateListener>)
        public
          constructor create;
          Destructor Destroy; Override;
          procedure onCallForwardingIndicatorChanged(cfi: Boolean); cdecl;
          procedure onCallStateChanged(state: Integer; incomingNumber: JString); cdecl;
          ......... (and other procedures of main class)
    end;
    Implementation

    Constructor TPL.Create;
        Begin

        End;
    Destructor TPL.Destroy;
        Begin
            Inherited Destroy;
        End;
    procedure TPL.onCallForwardingIndicatorChanged(cfi: Boolean);
        Begin

        End;
   .........  

和主要代码中:

procedure TForm1.Button1Click(Sender: TObject);
var
  obj: JObject;
  TelephonyManager: JTelephonyManager;
  Tj:TPL;
begin
  obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  if (obj<>nil) then
      begin
          TelephonyManager := TJTelephonyManager.Wrap((obj as ILocalObject).GetObjectID);
          if (TelephonyManager<>nil) then
              begin
                  Tj := TPL.create;
                  TelephonyManager.listen(Tj, TJPhoneStateListener.JavaClass.LISTEN_CELL_INFO or TJPhoneStateListener.JavaClass.LISTEN_CELL_LOCATION);  <------ Error is here
              end;
      end;
end;

在编译时我会遇到此错误:

[DCC Error] main.pas(61): E2010 Incompatible types: 'JPhoneStateListener' and 'TPL'

感谢@nolaspeaker

我如下编辑我的代码:

unit main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.ExtCtrls, FMX.Helpers.Android, RTL.Controls,
  Androidapi.JNI.Os, Androidapi.Helpers, Androidapi.JNI.Telephony,
  Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes,
  Androidapi.JNIBridge, Androidapi.JNI.App;

type
  TPhoneStateListener = class;

  TForm1 = class(TForm)
    Button1: TButton;
    RTLFixer1: TRTLFixer;
    Memo1: TMemo;
    Timer1: TTimer;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Timer1Timer(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure InitPSListener(Start: Boolean);
  private
    PhoneStateListener: TPhoneStateListener;
    FSignal: Integer;
    { Private declarations }
  public
    { Public declarations }
  end;

//type
  TPhoneStateListener = class(TJavaLocal, JICustomPhoneStateListener)
  private
    [weak]
    FParent : TForm1;
  public
    constructor Create(AParent : TForm1);
    procedure onCallForwardingIndicatorChanged(cfi: Boolean); cdecl;
    procedure onCallStateChanged(state: Integer; incomingNumber: JString); cdecl;
    procedure onCellInfoChanged(cellInfo: JList); cdecl;
    procedure onCellLocationChanged(location: JCellLocation); cdecl;
    procedure onDataActivity(direction: Integer); cdecl;
    procedure onDataConnectionStateChanged(state: Integer); overload; cdecl;
    procedure onDataConnectionStateChanged(state: Integer; networkType: Integer); overload; cdecl;
    procedure onMessageWaitingIndicatorChanged(mwi: Boolean); cdecl;
    procedure onServiceStateChanged(serviceState: JServiceState); cdecl;
    procedure onSignalStrengthChanged(asu: Integer); cdecl;
    procedure onSignalStrengthsChanged(signalStrength: JSignalStrength); cdecl;
  end;

var
  Form1: TForm1;

implementation
{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}

{$R *.LgXhdpiPh.fmx ANDROID}



procedure TForm1.Button1Click(Sender: TObject);
begin
  InitPSListener(True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  InitPSListener(False);
end;

procedure TForm1.InitPSListener(Start: Boolean);
var
  obj: JObject;
begin

  if PhoneStateListener = nil then
    PhoneStateListener := TPhoneStateListener.Create(Self);
  obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  if obj <> nil then
  begin
    CallinUIThread(procedure
    var
      TelephonyManager: JTelephonyManager;
      PSL: JCustomPhoneStateListener;
    begin
      TelephonyManager := TJTelephonyManager.Wrap(obj);
      PSL := TJCustomPhoneStateListener.JavaClass.init(PhoneStateListener);
      if Start then
        TelephonyManager.listen(PSL,
          TJPhoneStateListener.JavaClass.LISTEN_SIGNAL_STRENGTHS)
//          or TJPhoneStateListener.JavaClass.LISTEN_SIGNAL_STRENGTH
//          or TJPhoneStateListener.JavaClass.LISTEN_DATA_ACTIVITY
      else
        TelephonyManager.listen(PSL,
          TJPhoneStateListener.JavaClass.LISTEN_NONE)
    end
    );
  end;

end;


{ TPhoneStateListener }

constructor TPhoneStateListener.Create(AParent: TForm1);
begin
  inherited Create;
  FParent := AParent;
end;

procedure TPhoneStateListener.onCallForwardingIndicatorChanged(cfi: Boolean);
begin
end;

procedure TPhoneStateListener.onCallStateChanged(state: Integer;
  incomingNumber: JString);
begin
end;

procedure TPhoneStateListener.onCellInfoChanged(cellInfo: JList);
begin
end;

procedure TPhoneStateListener.onCellLocationChanged(location: JCellLocation);
begin
end;

procedure TPhoneStateListener.onDataActivity(direction: Integer);
begin
end;

procedure TPhoneStateListener.onDataConnectionStateChanged(state: Integer);
begin
end;

procedure TPhoneStateListener.onDataConnectionStateChanged(state,
  networkType: Integer);
begin
end;

procedure TPhoneStateListener.onMessageWaitingIndicatorChanged(mwi: Boolean);
begin
end;

procedure TPhoneStateListener.onServiceStateChanged(
  serviceState: JServiceState);
begin
end;

procedure TPhoneStateListener.onSignalStrengthChanged(asu: Integer);
begin
end;

procedure TPhoneStateListener.onSignalStrengthsChanged(
  signalStrength: JSignalStrength);
begin
  FParent.onSignalStrengthsChanged(signalStrength);  <---- error is here
end;

procedure TForm1.OnSignalStrengthsChanged(signalStrength: JSignalStrength);
begin
  FSignal := signalStrength.getLevel; // 0 - 4   <----- and is here
end;

end.

但仍然有错误:
[DCC错误] main.pas(249):E2003未声明的标识符:'onSignalStrengthsChanged'

1 个答案:

答案 0 :(得分:1)

这里有太多要解释的内容。希望这可以帮助您解决编码问题。

type

  TPhoneStateListener = class;

  TfrmAppMain = class(TForm)
    HeaderToolBar: TToolBar;
    ToolBarLabel: TLabel;
    TabControl1: TTabControl;

..

  private
    PhoneStateListener: TPhoneStateListener;
    FSignal: Integer;

..

  TPhoneStateListener = class(TJavaLocal, JICustomPhoneStateListener)
  private
    [weak]
    FParent : TfrmAppMain;
  public
    constructor Create(AParent : TfrmAppMain);
    procedure onCallForwardingIndicatorChanged(cfi: Boolean); cdecl;
    procedure onCallStateChanged(state: Integer; incomingNumber: JString); cdecl;
    procedure onCellInfoChanged(cellInfo: JList); cdecl;
    procedure onCellLocationChanged(location: JCellLocation); cdecl;
    procedure onDataActivity(direction: Integer); cdecl;
    procedure onDataConnectionStateChanged(state: Integer); overload; cdecl;
    procedure onDataConnectionStateChanged(state: Integer; networkType: Integer); overload; cdecl;
    procedure onMessageWaitingIndicatorChanged(mwi: Boolean); cdecl;
    procedure onServiceStateChanged(serviceState: JServiceState); cdecl;
    procedure onSignalStrengthChanged(asu: Integer); cdecl;
    procedure onSignalStrengthsChanged(signalStrength: JSignalStrength); cdecl;
  end;

...

procedure TfrmAppMain.FormShow(Sender: TObject);
begin
  InitPSListener(True);
end;

..

procedure TfrmAppMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  InitPSListener(False);
end;

...

procedure TfrmAppMain.InitPSListener(Start: Boolean);
var
  obj: JObject;
begin

  if PhoneStateListener = nil then
    PhoneStateListener := TPhoneStateListener.Create(Self);
  obj := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  if obj <> nil then
  begin
    CallinUIThread(procedure
    var
      TelephonyManager: JTelephonyManager;
      PSL: JCustomPhoneStateListener;
    begin
      TelephonyManager := TJTelephonyManager.Wrap(obj);
      PSL := TJCustomPhoneStateListener.JavaClass.init(PhoneStateListener);
      if Start then
        TelephonyManager.listen(PSL,
          TJPhoneStateListener.JavaClass.LISTEN_SIGNAL_STRENGTHS)
//          or TJPhoneStateListener.JavaClass.LISTEN_SIGNAL_STRENGTH
//          or TJPhoneStateListener.JavaClass.LISTEN_DATA_ACTIVITY
      else
        TelephonyManager.listen(PSL,
          TJPhoneStateListener.JavaClass.LISTEN_NONE)
    end
    );
  end;

end;

...

{ TPhoneStateListener }

constructor TPhoneStateListener.Create(AParent: TfrmAppMain);
begin
  inherited Create;
  FParent := AParent;
end;

procedure TPhoneStateListener.onCallForwardingIndicatorChanged(cfi: Boolean);
begin
end;

procedure TPhoneStateListener.onCallStateChanged(state: Integer;
  incomingNumber: JString);
begin
end;

procedure TPhoneStateListener.onCellInfoChanged(cellInfo: JList);
begin
end;

procedure TPhoneStateListener.onCellLocationChanged(location: JCellLocation);
begin
end;

procedure TPhoneStateListener.onDataActivity(direction: Integer);
begin
end;

procedure TPhoneStateListener.onDataConnectionStateChanged(state: Integer);
begin
end;

procedure TPhoneStateListener.onDataConnectionStateChanged(state,
  networkType: Integer);
begin
end;

procedure TPhoneStateListener.onMessageWaitingIndicatorChanged(mwi: Boolean);
begin
end;

procedure TPhoneStateListener.onServiceStateChanged(
  serviceState: JServiceState);
begin
end;

procedure TPhoneStateListener.onSignalStrengthChanged(asu: Integer);
begin
end;

procedure TPhoneStateListener.onSignalStrengthsChanged(
  signalStrength: JSignalStrength);
begin
  FParent.onSignalStrengthsChanged(signalStrength);
end;

procedure TfrmAppMain.OnSignalStrengthsChanged(signalStrength: JSignalStrength);
begin
  FSignal := signalStrength.getLevel; // 0 - 4
end;