问题就PhoneStateListener听OnCellInfoChanged在delphi fir

2019-11-08 13:36发布


可以理解的,如果帮我。 我写德尔福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.

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

Answer 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;


文章来源: Problem on PhoneStateListener to listen to OnCellInfoChanged in delphi firemonkey