2

I try to write an Android service, which will use IdHTTP component to send messages to the server. I just put IdHTTP1 on TAndroidServiceDM and try to Run app on Phone (Android 4.2.2) I get error: "Project LocSensDemo.apk raised exception class Exception with message 'Activity not found, maybe you are in a service'" in FMX.Platform.Android line 1792 "Activity := TAndroidHelper.Activity;" and then "Project LocSensDemo.apk raised exception class Segmentation fault (11)". Service crashes.

How to solve this problem?

mjn
  • 36,362
  • 28
  • 176
  • 378
Rusland
  • 159
  • 2
  • 13
  • The `Activity` error has nothing to do with Indy, it is an Embarcadero problem. The `TAndroidHelper.Activity` property does not work in a service. As for the `Segmentation fault` error, you have not provided any details to diagnose that with. Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that reproduces the error. – Remy Lebeau Feb 01 '16 at 22:59
  • Here is a minimal project: App and Service https://mega.nz/#!VdJliLSR!_o2kTZJeMNRwP8lbaazbs4zdsbkXY1vx6gW3wPWspCI – Rusland Feb 05 '16 at 12:36

1 Answers1

0

The Activity not found exception occurs when the TAndroidHelper.Activity property is accessed inside a service project. That property is not usable in a service (see RSP-13381). Anything that uses TAndroidHelper.Activity in a service will crash at runtime. Line 1792 in the FMX.Platform.Android unit is inside of FMX.Platform.Android.TWindowManager.RetrieveContentRect(). What are you accessing in a service that is using TWindowManager? Services don't have UIs, you should not be touching anything UI-related to begin with.

If your service code needs access to an Android Context object, such as to call getContentResolver(), you can use the TAndroidHelper.Context property, or your service's own JavaService property (Android's Service class derives from Context).

As for the Segmentation fault exception, you haven't provided any details about that error, or shown what code is causing it, so there is no way for anyone here to diagnose it for you.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for reply. I use only IdHTTP component, northing else. I think the problem is here: then i put IdHTTP to service in 'uses' appears IdComponent unit. Chain using: IdComponent -> IdStack -> FMX.Helpers.Android -> Androidapi.Helpers which give me "Activity not found, maybe you are in a service" exception. – Rusland Feb 05 '16 at 12:12
  • You said the "Activity not found" error is occurring in the `FMX.Platform.Android` unit on line 1792, which is inside the FMX `TWindowManager` class, which Indy does not use. The error is raised by the `TAndroidHelper.Activity` property, which Indy does not use. You mention `IdStack`, which does use `FMX.Android.Helpers`, but it does not use `TAndroidHelper.Activity`, it uses `TAndroidHelper.Context`, which is safe in a service. Prior to Seattle, `IdStack` does use `SharedActivityContext()`, but that does not raise the error, and pre-Seattle versions do not support Android services anyway. – Remy Lebeau Feb 05 '16 at 17:17
  • Can you debug the Android app/service can get the actual call stack when the error occurs? I need to see the chain of calls that leads up to `TAndroidHelper.Activity` being used. – Remy Lebeau Feb 05 '16 at 17:20