Jump to content
Sign in to follow this  
Delpher2600

Application crashed when i connect Android Service

Recommended Posts

Hi. I am making Android app, and i add Android Service to project, guided by this video:

Code in the Android Service: 

unction TDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
end;

Calling from main project: 

FService : TLocalServiceConnection;
FService := TLocalServiceConnection.Create;
FService.StartService('F');

But when i lainch my application, it is crashing at once at start.  When i launch my app under debugging, Delphi writing me:

Quote

raised exception class EJNIException with message

Full text on the attachment. 

Also, debugger get this code:

Quote

{$IFDEF ANDROID}
procedure DoRaiseJNIExceptionCallBack(const JNIExceptionClassName: string; const Msg: string);
begin
  raise EJNIException.CreateWithClassName(JNIExceptionClassName, Msg);
end;
{$ENDIF}

Quote

procedure TPlatformAndroid.UnbindAppGlueEvents;
var
  AndroidAppGlue: TAndroidApplicationGlue;
begin
  AndroidAppGlue := PANativeActivity(System.DelphiActivity)^.instance;
  if AndroidAppGlue <> nil then
  begin
    AndroidAppGlue.OnApplicationCommandEvent := nil;
    AndroidAppGlue.OnContentRectEvent := nil;
    AndroidAppGlue.OnInputEvent := nil;
  end;
end;

I am using Delphi 10.4 CE. 

 

 

изображение_2023-02-15_014815693.png

Edited by Delpher2600

Share this post


Link to post
1 hour ago, Delpher2600 said:

Calling from main project:

That doesn't show where in the project the code is executed. In later versions of Android, you a service needs to be started when the application is in the foreground, so you may need to execute the code that starts it in the OnShow event of the main form, or possibly add a listener for the BecameActive application event, and execute it then.

 

Also, this code:

1 hour ago, Delpher2600 said:

FService.StartService('F');

Does not match the exception in the image, since 'Z' <> 'F'. I assume a typo, or you renamed the service.

 

Share this post


Link to post
5 minutes ago, Dave Nottage said:

n later versions of Android, you a service needs to be started when the application is in the foreground, so you may need to execute the code that starts it in the OnShow event of the main form, or possibly add a listener for the BecameActive application event, and execute it then.

How make it?

5 minutes ago, Dave Nottage said:

I assume a typo, or you renamed the service.

Yes, it's was renamed. 

Share this post


Link to post
1 minute ago, Delpher2600 said:

How make it?

Are you saying you do not understand this part?

2 minutes ago, Delpher2600 said:

you may need to execute the code that starts it in the OnShow event of the main form

 

Share this post


Link to post
1 minute ago, Dave Nottage said:

Are you saying you do not understand this part?

 

1 minute ago, Dave Nottage said:

you may need to execute the code that starts it in the OnShow event of the main form

I'm using this code: 

FService : TLocalServiceConnection;
FService := TLocalServiceConnection.Create;
FService.StartService('F');

In OnTimer of TTimer. So, i think not if using OnShow may change somebody. 

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×