0

This is my code:

unit Unit1;

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,
  Androidapi.JNI.Media,
  Androidapi.JNI.JavaTypes,
  Androidapi.Helpers,
  Androidapi.JNI.GRaphicsContentViewText,
  Androidapi.JNIBridge,
  Androidapi.JNI.os,
  Androidapi.JNI.App,
  System.Messaging;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    MPManager: JMediaProjectionManager;
    FMessageSubscriptionID: integer;
    mp: JMediaProjection;
    MPObj: JObject;
    procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
    function OnActivityResult(RequestCode, ResultCode: integer;
      Data: JIntent): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage
    (TMessageResultNotification, HandleActivityMessage);

  MPObj := TAndroidHelper.Context.getSystemService
    (TJContext.JavaClass.MEDIA_PROJECTION_SERVICE);
  MPManager := TJMediaProjectionManager.Wrap((MPObj as ILocalObject)
    .GetObjectID);
  TAndroidHelper.Activity.startActivityForResult
    (MPManager.createScreenCaptureIntent, 100);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MPManager := nil;
end;

procedure TForm1.HandleActivityMessage(const Sender: TObject;
  const M: TMessage);
begin
  if M is TMessageResultNotification then
    OnActivityResult(TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
end;

function TForm1.OnActivityResult(RequestCode, ResultCode: integer;
  Data: JIntent): Boolean;

begin
  TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification,
    FMessageSubscriptionID);

  if ResultCode = -1 then
  begin
    mp := MPManager.getMediaProjection(ResultCode, Data);
    if mp <> nil then
    begin
      Label1.Text := 'OK';
    end;
  end;
end;

end.

Application freezes while getting MediaProjection with "mp := MPManager.getMediaProjection(ResultCode, Data);". Help, please. what am I doing wrong? A confirmation window appears, and after the user gives permission to record the screen, the application freezes. Help, please. what am I doing wrong?

Pavel
  • 1
  • 1
  • 1
    Not sure if this is applies to your case, but see the docs regarding SDK version Q (Android 10), [here](https://developer.android.com/reference/android/media/projection/MediaProjectionManager#getMediaProjection(int,%20android.content.Intent)). In order to determine whether it does and/or as to exactly what is happening, I suggest using a logcat viewer, and probably [add some logging to your code](https://docs.code-kungfu.com/books/debugging-tips-and-tricks/page/adding-log-calls-to-your-code-and-how-to-view-them). – Dave Nottage Mar 28 '23 at 19:11

0 Answers0