[Make a new project for Android service.]

File→New→Other

NewItems→Delphi Project→Android Service.

After making the above selection, click OK.

 

Select a "Local Service" and click OK.

TDM class that inherits TAndroidService will be created.

Place the TNotificationCenter.

Create an OnStartCommand event and write the code.

////
function TDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
var
  a_notification_: TNotification;
begin
  a_notification_ := NotificationCenter1.CreateNotification;
  try
    a_notification_.Name := 'Embarcadero Android Service1';
    a_notification_.Title := 'Android Service';
    a_notification_.AlertBody := 'this is the Embarcadero Android Service.';
    a_notification_.FireDate := IncSecond(Now, 1);
    NotificationCenter1.ScheduleNotification(a_notification_);
  finally
    a_notification_.Free;
  end;
  JavaService.stopSelf;
  Result := TJService.JavaClass.START_STICKY;
end;

I saved it with the name p1.

 


 

[Create a C++Builder project]

Add it to the Project Group.

 

Select New Items→Mulch-Device Application .

 

Select Android target platform. 

 Place TButton, TMemo, TNotificationCenter in the form.

Add Android service.

Right-click the Android target device.

 

Add Android Service Dialog comes out.

Select automatic.

Specify a directory of service project that you saved.

 

Make sure it is correct.

 

[Create a button click event]

//---------------------------------------------------------------------------
#include <System.Android.Service.hpp>
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
	TLocalServiceConnection::StartService("p1");
}

[Create NotificationCenter1 receive events.]

Write the code in OnReceiveLocalNotification.

//---------------------------------------------------------------------------
void __fastcall TForm2::NotificationCenter1ReceiveLocalNotification(TObject *Sender,
          TNotification *ANotification)
{
//    NotificationCenter1->OnReceiveLocalNotification
	Memo1->Lines->Append("Title " + ANotification->Title);
	Memo1->Lines->Append("AlertBody " + ANotification->AlertBody);

}
//---------------------------------------------------------------------------

 


 

[Execution]

Tap Button1 to start Android Service("p1").

Write TNotification received from Service to Memo1→Lines

Anonymous