Catch Lock Screen Event

330 views Asked by At

Good day. I am writing in C ++ Builder in Embarcadero Xe8. I do mobile application project on Ios and android and faced such problem: I can not catch the phone lock screen event. I used to always do so:

    bool TForm1::HandleApp(TApplicationEvent a, TObject *x)
{
    if (a == TApplicationEvent::EnteredBackground)
    {
        MediaPlayer1->Stop();
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  _di_IFMXApplicationEventService a;
   if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXApplicationEventService), &a))
   {
    a->SetApplicationEventHandler(TForm1::HandleApp);
   }
}

But an error:

\Unit1.cpp(33): cannot initialize a parameter of type 'TApplicationEventHandler' (aka 'bool (closure *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong) __attribute((pcs("aapcs-vfp")))') with an lvalue of type 'bool (__closure *)(Fmx::Platform::TApplicationEvent, System::TObject __borland_class *__strong)' FMX.Platform.hpp(252): passing argument to parameter 'AEventHandler' here

I Do not know what else to try to do! Could you please help me?

1

There are 1 answers

0
Remy Lebeau On

Your HandleApp() method is missing the __fastcall calling convention:

bool __fastcall  TForm1::HandleApp(TApplicationEvent a, TObject *x)

Also, your call to SetApplicationEventHandler() needs to be like this instead:

a->SetApplicationEventHandler(&HandleApp);

This is important because the event handler is a __closure, so it carries two pointers inside of it - a pointer to the class method to call, and a pointer to the object instance that the method is called on (the this value of the method). When you pass the handler by class name only, the compiler doesn't know which object instance to act on, and thus cannot populate the __closure. The syntax above allows the compiler to see that HandleApp should be associated with the Form1 object.