Jump to content
ToddFrankson

Android and services...

Recommended Posts

I need some help in figuring out services....

 

I will be writing an app that needs to stay running, and every certain interval of time (at least 15 minutes or more-user configurable), open a file(in the apps folder) read it and then close the file(there's more to it but that is basics).

 

 

I believe a foreground service is best, with it sticky, and start on boot.... I have those pieces.

 

So the questions I have are the following:

 

Like a weather app, I'd like it to stay in the Notification Panel at the top.  How does that work? anyone have sample code?

See the image.....How do I make my App stay like that in the notification panel, and then when the panel is "closed" it displays the current temperature.  Has anyone done that in Delphi?

 

I also will have the interface allowing to see the contents of the file the service opens, I believe I have some samples for that, but if it helps it will be a few configuration text items, and the UI will also open the same file, blocking the Service from doing so when the UI is launched.  I believe I have that covered as well....

 

 

Screenshot_20241109_124015_One UI Home.jpg

Share this post


Link to post
On 11/10/2024 at 5:16 AM, ToddFrankson said:

How do I make my App stay like that in the notification panel

You'd need to create the notification using the underlying classes, since the notification implementation in Delphi is yet to support "sticky" notifications. You could use the code in the TNotificationCenterAndroid.CreateNativeNotification method in rtl\common\System.Android.Notification.pas as a guide, and use the setOngoing method to make it "sticky".

 

It's not exactly clear from your post what else you need solved.

Edited by Dave Nottage
  • Like 1

Share this post


Link to post
On 11/10/2024 at 2:26 PM, Dave Nottage said:

You'd need to create the notification using the underlying classes, since the notification implementation in Delphi is yet to support "sticky" notifications. You could use the code in the TNotificationCenterAndroid.CreateNativeNotification method in rtl\common\System.Android.Notification.pas as a guide, and use the setOngoing method to make it "sticky".

 

It's not exactly clear from your post what else you need solved.

Not necessarily solved.  Just trying to understand the process..........

I've read that foreground services have some specific permissions that need to be requested, along with specific reasons why.

I think I have a grasp on that now...

 

As for the notifications, I can get that working by modifying your Kastri Demo Actually.....I used the JNotification_Builder and  JNotificationManager to create the first Notification.

For updates, I used the  JNotificationCompat_Builder

 

Still having issues getting the App Icon Using the following:

 

var

   MainAppContext: JContext;
  AppIcon: Integer;
Begin

// Get the main application context by package name

MainAppContext := TAndroidHelper.Context.getApplicationContext;

// Fetch the application icon from the main application context
AppIcon := MainAppContext.getPackageManager.getApplicationInfo( MainAppContext.getPackageName, 0).icon;

......

//other code related to building either the Notification_Builder or JNotificationCompat_Builder.

....

End;

 

The Icon is always a white box.
 

 

 

 

 

Share this post


Link to post
16 minutes ago, ToddFrankson said:

Still having issues getting the App Icon Using the following:

As I mentioned in my first reply, you could use the code from TNotificationCenterAndroid.CreateNativeNotification as an example. Inside that is this:

  function GetDefaultIconID: Integer;
  begin
    Result := TAndroidHelper.GetResourceID('drawable/ic_notification');
    if Result = 0 then
      Result := TAndroidHelper.Context.getApplicationInfo.icon;
  end;

 

Share this post


Link to post

@Dave Nottage

I got away from this for a bit, but I am now looking back at it.

 

It Seems the 64 bit version doesn't compile.  I have tried to add the dw-kastri-base-3.0.0.jar to the 64 bit version but it doesn't "Add".  I can compile the libforegroundservice.o just fine.

 

I get the following errors when I try to run it:

[PAClient Error] Error: E7688 Error in .\Android64\Debug\ForegroundService-dexed.jar:classes.dex:
[PAClient Error] Error: E7688 Type com.embarcadero.services.ForegroundService$LocalBinder is defined multiple times: .\Android64\Debug\ForegroundService-dexed.jar:classes.dex, .\Android64\Debug\ForegroundService-dexed.jar:classes.dex
[PAClient Error] Error: E7688 com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: .\Android64\Debug\ForegroundService-dexed.jar:classes.dex
[PAClient Error] Error: E7688     at Version.fakeStackEntry(Version_8.2.33.java:0)
[PAClient Error] Error: E7688     at com.android.tools.r8.T.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:5)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:82)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:32)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:31)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.S0.b(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:2)
[PAClient Error] Error: E7688     at com.android.tools.r8.D8.run(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6)
[PAClient Error] Error: E7688     at com.embarcadero.dexer.DexerCommand$MergeCommand.run(DexerCommand.kt:41)
[PAClient Error] Error: E7688     at picocli.CommandLine.executeUserObject(CommandLine.java:2026)
[PAClient Error] Error: E7688     at picocli.CommandLine.access$1500(CommandLine.java:148)
[PAClient Error] Error: E7688     at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)
[PAClient Error] Error: E7688     at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)
[PAClient Error] Error: E7688     at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)
[PAClient Error] Error: E7688     at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)
[PAClient Error] Error: E7688     at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)
[PAClient Error] Error: E7688     at picocli.CommandLine.execute(CommandLine.java:2170)
[PAClient Error] Error: E7688     at com.embarcadero.dexer.Main.main(Main.kt:10)
[PAClient Error] Error: E7688 Caused by: com.android.tools.r8.utils.b: Type com.embarcadero.services.ForegroundService$LocalBinder is defined multiple times: .\Android64\Debug\ForegroundService-dexed.jar:classes.dex, .\Android64\Debug\ForegroundService-dexed.jar:classes.dex
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.Q2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:21)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:54)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:10)
[PAClient Error] Error: E7688     at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2056)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.D2.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6)
[PAClient Error] Error: E7688     at com.android.tools.r8.graph.m4$a.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:6)
[PAClient Error] Error: E7688     at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:61)
[PAClient Error] Error: E7688     at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:12)
[PAClient Error] Error: E7688     at com.android.tools.r8.dex.c.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:9)
[PAClient Error] Error: E7688     at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:45)
[PAClient Error] Error: E7688     at com.android.tools.r8.D8.d(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:17)
[PAClient Error] Error: E7688     at com.android.tools.r8.D8.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:35)
[PAClient Error] Error: E7688     at com.android.tools.r8.utils.S0.a(R8_8.2.33_429c93fd24a535127db6f4e2628eb18f2f978e02f99f55740728d6b22bef16dd:28)
[PAClient Error] Error: E7688     ... 12 more

Share this post


Link to post
59 minutes ago, ToddFrankson said:

It Seems the 64 bit version doesn't compile.  I have tried to add the dw-kastri-base-3.0.0.jar to the 64 bit version but it doesn't "Add"

If you're using Delphi 12.2, you won't need to add it to the 64-bit target if you have already added it to the 32-bit target - it will still be compiled in.

1 hour ago, ToddFrankson said:

I get the following errors when I try to run it:

[PAClient Error] Error: E7688 Error in .\Android64\Debug\ForegroundService-dexed.jar:classes.dex:
[PAClient Error] Error: E7688 Type com.embarcadero.services.ForegroundService$LocalBinder is defined multiple times: .\Android64\Debug\ForegroundService-dexed.jar:classes.dex, .\Android64\Debug\ForegroundService-dexed.jar:classes.dex
[PAClient Error] Error: E7688 com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: .\Android64\Debug\ForegroundService-dexed.jar:classes.dex

It appears you have somehow added the .jar that contains the service, twice. Can you detail what you have in the libraries nodes for both Android 32-bit and Android 64-bit in Project Manager? Just the entries under the last of the "system" jars, which would be: work-runtime-2.7.0.dex.jar, if you have Delphi 12.2. Selecting a jar should show the full path to it, in the status bar of Project Manager.

Share this post


Link to post
3 hours ago, Dave Nottage said:

It appears you have somehow added the .jar that contains the service, twice. Can you detail what you have in the libraries nodes for both Android 32-bit and Android 64-bit in Project Manager? Just the entries under the last of the "system" jars, which would be: work-runtime-2.7.0.dex.jar, if you have Delphi 12.2. Selecting a jar should show the full path to it, in the status bar of Project Manager.

 

32bit.jpg

64bit.jpg

Share this post


Link to post

You have ForegroundService.jar in both the 32-bit and 64-bit targets - when you compile for 64-bit it's attempting to include both into the one app. (Note that for the "system" jars, it just uses those for the target platform). If you're going to compile for either, remove the one from the 64-bit target.

Share this post


Link to post
Just now, Dave Nottage said:

You have ForegroundService.jar in both the 32-bit and 64-bit targets - when you compile for 64-bit it's attempting to include both into the one app. (Note that for the "system" jars, it just uses those for the target platform). If you're going to compile for either, remove the one from the 64-bit target.

Ah....

 

I also noted that Delphi's "Clean" doesn't remove a lot of stuff from build directories.  It Seems a goo Select all/Delete helps too.

 

Thank you sir!

 

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

×