Android Question Help Fixing up an older app - Services

MrKim

Well-Known Member
Licensed User
Longtime User
I have an older, Android only app that I am updating a couple of features on.
I have two services that are primarily used for SMB (a secondary update is replacing smb1 with smb2 / ncifs-ng).
This is a dedicated tablet app. I am not really concerned about any other app or a phone or music player interrupting it.
The service only runs in the foreground (as I understand it). The user waits for the data to load.
From what I have read I added.
B4X:
Private Sub Service_Timeout
    Service.StopAutomaticForeground
End Sub
To each of the services.
I added
B4X:
SetServiceAttribute(SMBsm, android:foregroundServiceType, shortService)
SetServiceAttribute(SKUtils, android:foregroundServiceType, shortService)
To the manifest.
When I try to compile I get the following error:
B4X:
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (0.23s)
Linking resources    Error
AndroidManifest.xml:73: error: 'shortService' is incompatible with attribute foregroundServiceType (attr) flags [camera=64, connectedDevice=16, dataSync=1, location=8, mediaPlayback=2, mediaProjection=32, microphone=128, phoneCall=4].
AndroidManifest.xml:90: error: 'shortService' is incompatible with attribute foregroundServiceType (attr) flags [camera=64, connectedDevice=16, dataSync=1, location=8, mediaPlayback=2, mediaProjection=32, microphone=128, phoneCall=4].
error: failed processing manifest.
Here is the full manifest in case I have this in the wrong place:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
'5/13/18 - Changed target API to 26 from 19
'5/13/18 - added READ_EXTERNAL_STORAGE
AddManifestText(
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.PERMISSION_RECORD_AUDIO" />
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="34"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<meta-data
    android:name="com.google.android.gms.vision.DEPENDENCIES"
    android:value="barcode" /> 
android:windowSoftInputMode="stateHidden|adjustPan"
    )
SetServiceAttribute(SMBsm, android:foregroundServiceType, shortService)
SetServiceAttribute(SKUtils, android:foregroundServiceType, shortService)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetActivityAttribute(SaveFile, android:windowSoftInputMode, adjustPan|stateHidden)
SetActivityAttribute(Documents, android:windowSoftInputMode, adjustPan|stateHidden)
'End of default text.
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)

Thanks for any help.
Similar code seems to run just fine in an activity. I did the services because there were a lot of common routines that I did not want to repeat in every activity that used them.
It would be huge time sink right now to move this to B4X Pages. All I really need at this point is to update a couple of SQL queries. I just thought I would go ahead and get the SMB update out of the way at the same time.
But I digress.
I should also mention that I am disabling strict mode so that I can run smb on the main thread. It has never been an issue.
Can someone tell me how to make google happy so I can update my customers?
Thanks.
 
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
Some thoughts on your post.
It would be huge time sink right now to move this to B4X Pages.
I understand that letting go of old programming habits is difficult for all of us. You can solve every programming goal with a good deal of different programming technical means. Not every choice of a programming means is the most convenient. There are "best practices" rules for that. If you stick to them, you have minimal chance of encountering "challenges" and a big chance that someone else knows a solution for your "challenge". In the past years, I have ported a lot of B4A and B4J source code to B4XPages with minimal or no adjustment. And certainly not if you use B4X components. The community also cannot give you targeted advice and possible specific solutions without a specific question about the source code related to the question.

Giving advice involves a great deal of non-bindingness. The person giving advice is aware that the person receiving it will ignore it. However, I advise you to reconsider the motivation in the past to choose a certain program solution with the new possibility of today. In doing so, the following two points of attention stand out to me:

I did the services because there were a lot of common routines that I did not want to repeat in every activity that used them.
If you don't want to repeat things in every activity, why not streamline that repetitive activity into a separate module or class (B4XPage)? Besides improving maintainability, this simplifies the readability of the source code.

I should also mention that I am disabling strict mode so that I can run smb on the main thread.
One of the reasons for not running an activity on the main thread is to prevent the program from seemingly freezing, for example with network and database activities. Because if an activity is frozen for about 5 minutes, this is interpreted as the program being stopped. We do not know on which criteria Google tests the offered program, but you may run into this.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Some thoughts on your post.
Thanks for your thoughts. What I am trying to grasp is it OK to try and run SMB outside of a service? Will that even work. I really don't have a grasp of the implications of this. What I have had has been working fine for years with no complaints. I don't mind getting this stuff out of a service but I don't know if that is OK or will it cause other problems. If I do where do I put it? A standard class? An activity module? This is not my area of expertise and I really don't have weeks to study it. I have a bit of a crises here with one of our customers and I need to get a fiew SQL changes in and get it published.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
A lot of questions, and really we can't give you answer without details what and how you do it.
What I am trying to grasp is it OK to try and run SMB outside of a service?
My advice is, to use the standard way to do it. However, it can be working like a charm, or you will hit direct, or later maybe in rare situation with problems.

What I have had has been working fine for years with no complaints.
That's nice, however a lot of things has been change in Android: stricter restrictions, deprecation of libraries, and so on.

If I do where do I put it? A standard class? An activity module?

These are the AI main difference:

Standard Class

  • A template for creating objects with state (variables) and behavior (methods).
  • Holds a reference to its context, allowing event handling and method calls via CallSub.
  • Can be instantiated multiple times (e.g., Person objects).
  • Unlike code modules, classes maintain their own context and lifecycle.
Key Features:
  • Uses Class_Globals for variables.
  • Requires an Initialize method to set up instances.
  • Can be used across B4A, B4i, and B4J.
B4XPages
  • A cross-platform framework simplifying navigation and UI management in B4A, B4i, and B4J.
  • No special lifecycle: Pages are never paused or destroyed (unlike Android activities).
  • Eliminates common challenges like missed events, UI state loss, and complex activity management.

Key Features:
  • Unified navigation across platforms.
  • Direct access to public methods/variables of other pages.
  • Supports multiple page instances of the same class.
  • Requires B4A 10.0+, B4J 8.50+, B4i 6.80+.

Activity Module (B4A-Specific)

  • Android-specific and tied to the Activity lifecycle (e.g., Create, Resume, Pause).
  • Global variables reset when the activity is destroyed.
  • Requires CallSubDelayed for inter-activity communication.

Key Differences from B4XPages:
  • Lifecycle-dependent: Pauses/destroys on app backgrounding.
  • Complex state management: Requires Starter Service for shared data.
  • No cross-platform support (only for B4A).
You can use your own imagination to initialize a B4XPage, for example, but not display it. This allows you to refer to the variable in that B4XPage to fill it, to point to it and to request it again and/or to use routines and functions in that B4XPage. And all this without having to worry about all kinds of peculiarities of Android.

I have a bit of a crises here with one of our customers and I need to get a fiew SQL changes in and get it published.
Take a short time to look to Android Tutorial SQL tutorial for the newer SQLite use.

Edit: In addition to the previous overview, one more important thing: If you place a certain functionality in a B4XPage, you can easily reuse this functionality = B4XPage class in other programs.
 
Last edited:
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Are you running the lastest B4X with SDK 34. From you logs it looks like your not. Short service is fine to use (I use it). The error:
'shortService' is incompatible with attribute foregroundServiceType (attr) flags [camera=64, connectedDevice=16, dataSync=1, location=8, mediaPlayback=2, mediaProjection=32, microphone=128, phoneCall=4] is listing the available FGS and shortService is not listed therefore I would suggest you are not using SDK34.

An example from my manifest:

SetServiceAttribute(svc_data_sync, android:foregroundServiceType, "shortService")
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Are you running the lastest B4X with SDK 34. From you logs it looks like your not. Short service is fine to use (I use it). The error:
I am running B4A 13.10.
B4A Sdk Manager: 4.10 which says: SDK version: 6609375
Tip: if using shortService then you also need to handle the Service_Timeout event: https://www.b4x.com/android/forum/t...getsdkversion-34-and-services.162140/#content
Is there an example that uses shortservice? That would be really helpful.
After re-reading your post on android 14 and services for the 10th time I just realized - I can solve my problem by just moving this stuff to the Starter Service!? This app is so old it didn't even have starter so I just added it.
 
Upvote 0
Top