How to use Android Service(TAndroidService) in C++Builder

Posted by on in Programming

[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



About
Gold User, No rank,
Delphi / C++Builder blogger

Comments

  • Lena I2199
    Lena I2199 Tuesday, 12 September 2017

    Hi.
    How restart Android Service [project C++ Builder] if device reboot?
    Thank You very much!

  • haruyuki mohri
    haruyuki mohri Tuesday, 12 September 2017

    Does this mean making Android restart service?

  • Lena I2199
    Lena I2199 Wednesday, 13 September 2017

    My Android Service start after app start and click Button:
    void __fastcall TForm2::Button1Click(TObject *Sender)
    {
    TLocalServiceConnection::StartService("p1");
    }
    All fine.
    if device reboot my Android Service stop. How restart automatically my Android Service if device reboot?
    Thank You.

  • zs C30403
    zs C30403 Thursday, 8 June 2017

    I saved it with the name p1. Does the file name refer to the project name?

  • haruyuki mohri
    haruyuki mohri Tuesday, 12 September 2017

    The project name can be anything.
    The point is to make a service project first.

  • Lena I2199
    Lena I2199 Thursday, 11 May 2017

    I create new project and return "Find Static Library" dialog. It compile now.
    Do you have recommendations on how to make a service with TBeacon and С++Builder?
    I did not succeed:
    https://forums.embarcadero.com/thread.jspa?threadID=250255&stqc=true

  • Lena I2199
    Lena I2199 Friday, 12 May 2017

    This problem is not associated with the C++ Builder.
    Standard examples do not work either:
    https://forums.embarcadero.com/thread.jspa?threadID=250268&tstart=0
    Thanks again for the excellent article!

  • haruyuki mohri
    haruyuki mohri Friday, 12 May 2017

    Sorry. I had overlooked this comment.
    I have not tested TBeacon.

  • Lena I2199
    Lena I2199 Monday, 1 May 2017

    Hi.
    I do not see the window "Find Static Library" and I can not check "Remove the reference". I see only erorrs messages:
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -ldelphiandroidservices
    ***
    How to set "Remove the reference"? Thank You.

  • Lena I2199
    Lena I2199 Tuesday, 2 May 2017

    I do not see the window "Find Static Library" and I can not check "Remove the reference". I see only erorrs messages. How return window "Find Static Library"? So now I have: http://expert.um.la/err.jpg

  • haruyuki mohri
    haruyuki mohri Tuesday, 2 May 2017

    Will not the "Find Static Library" dialog come out when starting the build?

  • Lena I2199
    Lena I2199 Tuesday, 2 May 2017

    Yesterday I saw that window. I set Remove the reference and set "Don't ask me again". The compilation was successful. Today I don't see this window and got errors again. How to get window Find Static Library back now?

  • Lena I2199
    Lena I2199 Monday, 1 May 2017

    Hi.
    C++ Builder Berlin update 2.
    I try to add services in my project with TBeacon.
    When compile I got errors:
    Unabled to find static library: libdelphiandroidservises.a, libvclie.a....
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -ldelphiandroidservices
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lvclactnband
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lvclx
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lvcl
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lvclimg
    [ldandroid Error] C:\Users\Public\Documents\Embarcadero\Studio\18.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lvclie

  • haruyuki mohri
    haruyuki mohri Monday, 1 May 2017

    "Remove the reference "
    Please check this all.

  • Lena I2199
    Lena I2199 Monday, 1 May 2017

    Thank You very much!!!

  • haruyuki mohri
    haruyuki mohri Monday, 1 May 2017

    Please utilize by all means.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: