Android Question Android Version 14 upgrade problems

db0070

Active Member
Licensed User
Longtime User
I have an app that has been working fine for years, until I upgraded it to SDK 33. The upgraded version works fine on my Android S10 with Android version 12. There are users with Android version 14 who are complaining that when launching the app they get a blank screen and then message "isn't responding - Close app/wait" appears. I thought at first it was me using Scheduling Alarm Exact so I added this as recommeded by Erel: -

[CODE lang="b4x" title="Scheduling Alarm Exact"] Wait For (GetScheduleExactAlarmPermission) Complete (HasPermission As Boolean)
'StartServiceAtExact(ServiceName, NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)), True)
If HasPermission Then
StartServiceAtExact(ServiceName, NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)), True) 'yes it is StartServiceAtExact and we pass a receiver
[/CODE]

This is my manifest, could something perhaps be wrong with it?

[CODE lang="b4x" title="Manifest"]AddManifestText(
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddApplicationText(
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
)
CreateResourceFromFile(Macro, Core.NetworkClearText)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddApplicationText(<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />)
AddPermission(com.android.alarm.permission.SET_ALARM )
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
[/CODE]
 
I am guessing its related to this issue


Which was fixed in the latest B4A v12.80. Switch to the latest version if you are not using it already

 
Upvote 0
I am using v12.80. I have re-installed again B4A again and tested on a Android 14 emulator, and the user reported the same problem.
So, now I did a completely fresh install to C:\Android and C:\Java after removing. I have published the app and waiting for feedback.
 
Upvote 0
Ok, the user reports he is still getting the app not responding waiting/close message. I see no problem when installing on Android 14 emulator, any help please what else I can try?
 
Upvote 0
Do you use services?
I started to test my apps in Android 14 emulator and get an error because I use services instead receivers.
 
Upvote 0
op really needs to tell us what his app does and how he implemented it. random questions and guessing is a waste of time.
 
Upvote 0
The app uses location service, see my manifest in the first post. It also uses Set Alarms Exact, again I handle this as shown in my first post. My problem is that it works on an Android 14 emulator, but Application Not Responding error on a physical device as reported by a Android 14 user.

This is my service
[CODE lang="b4x" title="Service"]Sub Process_Globals
Public rp As RuntimePermissions
Public FLP As FusedLocationProvider
Private flpStarted As Boolean
End Sub

Sub Service_Create
FLP.Initialize("flp")
FLP.Connect
End Sub

Sub flp_ConnectionSuccess
Log("Connected to location provider")
End Sub

Sub flp_ConnectionFailed(ConnectionResult1 As Int)
Log("Failed to connect to location provider")
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground
End Sub

Public Sub StartFLP
Do While FLP.IsConnected = False
Sleep(1000)
Loop
Log(flpStarted)
If flpStarted = False Then
FLP.RequestLocationUpdates(CreateLocationRequest)
flpStarted = True
End If
End Sub

Private Sub flp_LocationChanged (Location1 As Location)
CallSub2(Main, "Location_Changed", Location1)
End Sub

Private Sub CreateLocationRequest As LocationRequest
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(10000)
lr.SetFastestInterval(lr.GetInterval / 2)
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
Return lr
End Sub

Public Sub StopFLP
If flpStarted Then
FLP.RemoveLocationUpdates
flpStarted = False
End If
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub

Sub Service_Destroy
StopFLP
End Sub[/CODE]
 
Upvote 0
Still struggling here.

According to https://developer.android.com/topic/performance/vitals/anr: -

An ANR is triggered for your app when one of the following conditions occur:
  • Input dispatching timed out: If your app has not responded to an input event (such as key press or screen touch) within 5 seconds.
  • Executing service: If a service declared by your app cannot finish executing Service.onCreate() and Service.onStartCommand()/Service.onBind() within a few seconds.
  • Service.startForeground() not called: If your app uses Context.startForegroundService() to start a new service in the foreground, but the service then does not call startForeground() within 5 seconds.
  • Broadcast of intent: (I don't use this in my app)
  • JobScheduler interactions: (I don't use this in my app)
Input dispatching time out I can rule out because the app does not even open when clicked on the icon on an Android 14 phone

There must be something wrong with my Service. I am have now changed from using the standard Service and added this "Starter_Service" module shown below. The Android 14 user says that when they open the app on the phone, there is a blank screen for about 5 or 6 seconds before the Wait/Kill message comes up. It works on an emulator, how do I debug this error short of buying the latest Android phone? Or do I have to convert my app to B4XPages?

[CODE lang="b4x" title="Service"]
#Region Service Attributes
#StartAtBoot: True
#End Region

Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Public rp As RuntimePermissions
Public FLP As FusedLocationProvider
Private flpStarted As Boolean
End Sub

Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
FLP.Initialize("flp")
FLP.Connect
lock.PartialLock
End Sub

Sub flp_ConnectionSuccess
Log("Connected to location provider")
End Sub

Sub flp_ConnectionFailed(ConnectionResult1 As Int)
Log("Failed to connect to location provider")
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
StartFLP
End Sub

Public Sub StartFLP
If flpStarted Then Return
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If

Do While FLP.IsConnected = False
Sleep(1000)
Loop
Log(flpStarted)
If flpStarted = False Then
FLP.RequestLocationUpdates(CreateLocationRequest)
flpStarted = True
End If
flpStarted = True
End Sub

Private Sub flp_LocationChanged (Location1 As Location)
CallSub2(Main, "Location_Changed", Location1)
End Sub

Private Sub CreateLocationRequest As LocationRequest
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(10000)
lr.SetFastestInterval(lr.GetInterval / 2)
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
Return lr
End Sub

Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tracking location", Body, Main)
Return notification
End Sub

Sub Service_Destroy
If flpStarted Then
FLP.RemoveLocationUpdates
flpStarted = False
End If
lock.ReleasePartialLock
End Sub
[/CODE]

The above service is called in Activity_Resume:-

[CODE lang="b4x" title="Activity"]
Sub Activity_Resume
Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
If HasPermission = False Then
Log("no permission")
ToastMessageShow("no permission", True)
Return
End If
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
StartService(Starter_Service)
'Starter.StartFLP
Else
ToastMessageShow("No permission...", True)
End If
.
.
.
End Sub

Private Sub CheckAndRequestNotificationPermission As ResumableSub
Dim p As Phone
If p.SdkVersion < 33 Then Return True
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim targetSdkVersion As Int = ctxt.RunMethodJO("getApplicationInfo", Null).GetField("targetSdkVersion")
If targetSdkVersion < 33 Then Return True
Dim NotificationsManager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
Dim NotificationsEnabled As Boolean = NotificationsManager.RunMethod("areNotificationsEnabled", Null)
If NotificationsEnabled Then Return True
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_POST_NOTIFICATIONS)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 'change to B4XPage_PermissionResult if B4XPages project
Log(Permission & ": " & Result)
Return Result
End Sub

[/CODE]
 
Upvote 0
I would move the code back to its separate service. Try this:

Code:
#Region  Service Attributes
' I don't see a reason for starting this at boot.
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private nid As Int = 1
    Private lock As PhoneWakeState
    Public rp As RuntimePermissions
    Public FLP As FusedLocationProvider
    Private flpStarted As Boolean
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    FLP.Initialize("flp")
    'FLP.Connect ' Don't do this here. If this, for some reason, takes a long time, you may get an ANR
    lock.PartialLock
End Sub

Sub flp_ConnectionSuccess
    Log("Connected to location provider")
    StartFLP ' Might as well use the event to call StartFLP properly
End Sub

Sub flp_ConnectionFailed(ConnectionResult1 As Int)
    Log($"Failed to connect to location provider. ConnectionResult = ${ConnectionResult1}"$)
    StopService(Me) ' Kill myself for cleanup
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("..."))
    'We are only going to call StartFLP directly if we're already connected
    If FLP.IsConnecting Then
        Log("Already trying to connect")
    Else if FLP.IsConnected Then
        StartFLP
    Else
        FLP.Connect
    End If
    'StartFLP
End Sub

'Taken straight from the example provided by @Erel
'https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
Private Sub CheckLocationSettingStatus As ResumableSub
    Dim f As LocationSettingsRequestBuilder
    f.Initialize
    f.AddLocationRequest(CreateLocationRequest)
    FLP.CheckLocationSettings(f.Build)
    Wait For flp_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
    Return LocationSettingsResult1
End Sub

'Modified to include extra code from
''https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
Public Sub StartFLP
    If flpStarted Then Return
    If rp.Check(rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then ' Use locally declared rp
        Log("No permission")
        Return
    End If
   'We only call StartFLP when FLP.isConnected = True. See Service_Start
'    Do While FLP.IsConnected = False
'        Sleep(1000)
'    Loop
    Log(flpStarted)
    If flpStarted = False Then
        'See
        Wait For (CheckLocationSettingStatus) Complete (SettingsResult As LocationSettingsResult)
        Dim sc As StatusCodes
        Select SettingsResult.GetLocationSettingsStatus.GetStatusCode
            Case sc.SUCCESS
                RequestLocationUpdates
            Case sc.RESOLUTION_REQUIRED
                'see https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
                Log("Location services require resolution")
                'Adjust implementation as needed
'                Dim rs As ResumableSub = CallSub2(Main, "ShowResolutionDialog", SettingsResult.GetLocationSettingsStatus)
'                Dim Result As Boolean
'                If rs.IsInitialized Then
'                    Wait For (rs) Complete (LocationSettingsUpdated As Boolean)
'                    Result = LocationSettingsUpdated
'                End If
'                If Result Then
'                    SettingsAreGood
'                Else
'                    SetState("Not enabled")
'                End If
            Case Else
                Log("Location services not available/enabled")
        End Select

    End If
    flpStarted = True
End Sub

Private Sub RequestLocationUpdates
    FLP.RequestLocationUpdates(CreateLocationRequest)
    flpStarted = True
End Sub

Private Sub flp_LocationChanged (Location1 As Location)
    If Not(IsPaused(Main)) Then ' Let's make sure Main is up and running
        CallSub2(Main, "Location_Changed", Location1)
    End If
End Sub

Private Sub CreateLocationRequest As LocationRequest
    Dim lr As LocationRequest
    lr.Initialize
    lr.SetInterval(10000)
    lr.SetFastestInterval(lr.GetInterval / 2)
    lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
    Return lr
End Sub

Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Tracking location", Body, Main)
    Return notification
End Sub

Sub Service_Destroy
    If flpStarted Then
        FLP.RemoveLocationUpdates
        FLP.Disconnect ' A little bit more of a cleanup
        flpStarted = False
    End If
    lock.ReleasePartialLock
    Service.StopForeground(nid)    ' Stopping what we started
End Sub
I tried to document the changes in the code. This is untested code. I hope the changes make sense and help you with the issue that you are experiencing.
 
Upvote 0
Thanks for your your suggestion, which I tested on the emulator which works. I tried on Platform_33_google_apis and Platform 34_google_apis emulator. But still the user with an Adroid 14 device says it gives the blank screen and ANR error. I am running B4A Sdk Manager: 4.10

The user's device which he was kind enough to give me is the following: -

One UI Version 6.0
Android Version 14
Google Play system update: 1 Nov 2023
Knox version: Knox 3.10, Knox API Level 37, HDM 2.0-7

What is frustrating is that it works on the emulator but not on the actual device - is this an emulator bug?
 
Upvote 0
Not to thread-hijack, but I've experienced something similar on my Samsung Galaxy S23 running Android 14. I've got a simple monitoring app that I wrote that has a dedicated service module that queries a remote API every 30 minutes and throws up a notification if anything's amiss otherwise it issues a StartServiceAt() for the next check and goes back to sleep. It has a "I'm running" notification up all the time similar to B4ABridge, and pops up a secondary notification if an issue needs addressing.

It ran flawlessly for a couple years before the phone upgraded to 14. After the phone upgraded the app would hang on a white screen and get a timeout error when launching. After researching here, I re-compiled under version 12.8 and targetsdk=33 and then the app launches and runs fine except....

Randomly, after running fine for several days, the "I'm running" notification will disappear. I attempt to re-launch the app from the apps list and then the app will lock up with a white screen. It never gets an app not responding error and I have to force-stop the app and re-launch it in order to get the UI back up.

Since this is just for an app for myself, I haven't bothered looking into it yet, but I figured I'd mention it in case it is relevant or helps @Erel diagnose it. If not, please ignore me :)
 
Last edited:
Upvote 0
Following your useful feedback, I have commented out the lines below in the Location_Changed function and sent the apk to my android 14 user and waiting for his comments back. Did you also find that it works on the emulator but hangs on the device?

[CODE lang="b4x" title="API" highlight="5"]
Dim API_KEY3 As String = "****************************************"
Dim res As String
Dim GetAddressJob As HttpJob
GetAddressJob.Initialize("GetAddress", Me)
GetAddressJob.Download2("https://maps.googleapis.com/maps/api/geocode/json", Array As String("latlng", lat & "," & lng,"key",API_KEY3))
Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
If GetAddressJob.Success Then
Dim jp As JSONParser
jp.Initialize(GetAddressJob.GetString)
[/CODE]
 
Upvote 0
This did not work either! On an Android 14 device it's still a blank screen with Close App/Wait when launching the app.
 
Upvote 0
I believe I found the cause, but don't know why. After sending my Android 14 user many apk files, I finally worked out that if I comment out the function below the Android 14 phone does not hang. My app is now working. Anyone shed some light on why this won't work on Android 14?

[CODE lang="b4x" title="StringUtils Lib"]
Sub SetTextSize(lbl As Label, txt As String)
Dim dt As Float
Dim limit = 0.5 As Float
Dim h As Int

lbl.Text = txt
lbl.TextSize = 72
dt = lbl.TextSize
h = stu.MeasureMultilineTextHeight(lbl, txt)
Do While dt > limit Or h > lbl.Height
dt = dt / 2
h = stu.MeasureMultilineTextHeight(lbl, txt)
If h > lbl.Height Then
lbl.TextSize = lbl.TextSize - dt
Else
lbl.TextSize = lbl.TextSize + dt
End If
Loop
lbl.TextSize = lbl.TextSize - 2
End Sub[/CODE]
 
Upvote 0
Connecting to an emulator (as I don't have an Android 14 device) I get this: -

** Activity (main) Create (first time) **
36, 6302
18, 1435
9, 382
4.5, 805
2.25, 603
1.125, 672
0.5625, 648
0.28125, 626
36, 522
18, 175
9, 46
4.5, 13
2.25, 19
1.125, 41
0.5625, 20
0.28125, 40
** Activity (main) Resume **
 
Upvote 0
Thanks for your your suggestion, which I tested on the emulator which works. I tried on Platform_33_google_apis and Platform 34_google_apis emulator. But still the user with an Adroid 14 device says it gives the blank screen and ANR error. I am running B4A Sdk Manager: 4.10

The user's device which he was kind enough to give me is the following: -

One UI Version 6.0
Android Version 14
Google Play system update: 1 Nov 2023
Knox version: Knox 3.10, Knox API Level 37, HDM 2.0-7

What is frustrating is that it works on the emulator but not on the actual device - is this an emulator bug?
Try changing the system font size to a bigger one and you should get ANR on the emulator (and any android 14 device)!
Problem comes from nonlinear font scaling: you set textsize to 20 and it reads back (for example) 16.
My algorythm to autosize labels set and read back from the label's textsize, just like yours, and at some point you set size+1 and it reads back size. It was sufficient to use a variable instead: change the variable, then assign the variable to textsize, problem solved.
 
Upvote 0
Back
Top