Android Question GPS permissions halts app

Roger C

Active Member
Licensed User
Longtime User
Hi all,

(added unfiltered logs in comments)

The first run after a fresh installation on a phone don't work. But all runs and updates following that works fine.

I have following code, in Main Activity_Create:
B4X:
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
StartService(GPSservice)   'Start GPS
If  MapFragment1.IsGooglePlayServicesAvailable = False Then
    ToastMessageShow("Please install Google Play Services.", True)
End If

First run of app (in debugmode but this happens in Release also) gives the Dialog about permission, click OK and it closes.
Then nothing happens... Either it's the Main-layout visible, but it's frozen, or just a dark screen.
I have to manually kill the app through Settings.
Next (and following) time I run the app in debug-mode everything is normal and it works without freezing.

When run in Release the app flashes for a second and then dies. Next run (and the following) it works fine again.
This happens every time I start it after I have terminated it; short flash - nothing, second run - normal

This is the log from Android 6.0.1 (Sony Xperia):
B4X:
** Service (starter) Start **
Starter.Service_Start
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Main paused. User closed=false
sending message to waiting queue (mapfragment1_ready)
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
** Activity (main) Resume **
And then nothing happens...

Log from Android 9 (Samsung G8), here I can terminate the app through the app, not by killing it but nothing happens after "** Activity (main) Resume **"
B4X:
** Service (starter) Start **
Starter.Service_Start
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Main paused. User closed=false
sending message to waiting queue (mapfragment1_ready)
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
User closed: App will terminate
Service destroyed
--------- beginning of crash
Copying updated assets files (19)
*** Service (starter) Create ***

The same run in Release mode:
B4X:
Logger connected to:  samsung SM-G950F
--------- beginning of system
--------- beginning of main
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
Main paused. User closed=true
Main Paused. App will terminate
Service destroyed

This is the code I terminate the app with:
B4X:
Starter.GPS1.Stop
       
        CancelScheduledService(GPSservice)
        Starter.InBackground = False
       
        CallSub(GPSservice,"Service_Destroy")
        CallSub(Starter,"Service_Destroy")

        Activity.Finish

So... First run after installation or termination of app, doesn't work. All following works.
Any suggestions what's wrong?
 
Last edited:

Roger C

Active Member
Licensed User
Longtime User
And heres the log without filter.
First run that freezes:

B4X:
Logger connected to:  Sony D6503
--------- beginning of main
ClassLoader referenced unknown path: /data/app/com.rocsoft.slclaim-1/lib/arm
Sending signal. PID: 18376 SIG: 9
ClassLoader referenced unknown path: /data/app/com.rocsoft.slclaim-1/lib/arm
common created.
Starting remote logger. Port: 13959
Use EGL_SWAP_BEHAVIOR_PRESERVED: true
<qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (I741a3d36ca)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 04/04/16 Mon
Local Branch: mybranch19053788
Remote Branch: quic/LA.BF.1.1.3_rb1.12
Local Patches: NONE
Reconstruct Branch: NOTHING
Initialized EGL, version 1.4
*** Debugger waiting for connection (0) ***
0xb34b2000 Launching thread(s), CPUs 4
After accept
*** Debugger waiting for connection (1) ***
Copying updated assets files (8)
*** Service (starter) Create ***
-----------------------------------------------------------------------------------
-
-  Start.  Time: 11:58:55
-
-----------------------------------------------------------------------------------
** Service (starter) Start **
Starter.Service_Start
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Making Creator dynamically
Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221
Selected remote version of com.google.android.gms.maps_dynamite, version >= 221
Dynamite loader version >= 2, using loadModule2NoCrashUtils
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000028/n/armeabi-v7a
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000028/n/armeabi
Google Play services client version: 12451000
Google Play services package version: 16091018
ClassLoader referenced unknown path: /system/framework/tcmclient.jar
Skipped 48 frames!  The application may be doing too much work on its main thread.
** Activity (main) Pause, UserClosed = false **
Main paused. User closed=false
sending message to waiting queue (mapfragment1_ready)
ignoring event: mapfragment1_camerachange
Timeline: Activity_idle id: android.os.BinderProxy@e4f3518 time:1409710582
Local module descriptor class for com.google.android.gms.googlecertificates not found.
Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
Selected remote version of com.google.android.gms.googlecertificates, version >= 4
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000027/n/armeabi-v7a
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000027/n/armeabi
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
** Activity (main) Resume **
Timeline: Activity_idle id: android.os.BinderProxy@e4f3518 time:1409723070
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
And the log from second run when it all works.

B4X:
Logger connected to:  Sony D6503
ClassLoader referenced unknown path: /data/app/com.rocsoft.slclaim-1/lib/arm
common created.
Starting remote logger. Port: 13959
Use EGL_SWAP_BEHAVIOR_PRESERVED: true
<qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (I741a3d36ca)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 04/04/16 Mon
Local Branch: mybranch19053788
Remote Branch: quic/LA.BF.1.1.3_rb1.12
Local Patches: NONE
Reconstruct Branch: NOTHING
Initialized EGL, version 1.4
*** Debugger waiting for connection (0) ***
0xb34b2000 Launching thread(s), CPUs 4
*** Debugger waiting for connection (1) ***
After accept
*** Service (starter) Create ***
-----------------------------------------------------------------------------------
-
-  Start.  Time: 12:00:50
-
-----------------------------------------------------------------------------------
** Service (starter) Start **
Starter.Service_Start
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Making Creator dynamically
Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:221
Selected remote version of com.google.android.gms.maps_dynamite, version >= 221
Dynamite loader version >= 2, using loadModule2NoCrashUtils
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000028/n/armeabi-v7a
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000028/n/armeabi
Google Play services client version: 12451000
Google Play services package version: 16091018
ClassLoader referenced unknown path: /system/framework/tcmclient.jar
Test.MakeListPoints:(MyMap) {Jobb_0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, Jobb_1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}, Jobb_2={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}, Jobb_3={Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}, Jobb_4={Trip=Jobb, Index=5, Lat=59.331008, Lon=18.059293, Name=Stockholm City, Products=512, Time=3}, Nära_0={Trip=Nära, Index=1, Lat=59.278026, Lon=18.006498, Name=JohanSkytteMagelung, Products=0, Time=3}, Nära_1={Trip=Nära, Index=2, Lat=59.278688, Lon=18.002283, Name=Hemköp, Products=0, Time=5}, Janne_0={Trip=Janne, Index=1, Lat=59.287814, Lon=18.006303, Name=Sulvägen, Products=8, Time=3}, Janne_1={Trip=Janne, Index=2, Lat=59.294118, Lon=18.004932, Name=Kontrollvägen, Products=8, Time=3}, Janne_2={Trip=Janne, Index=3, Lat=59.296122, Lon=18.012026, Name=Elektravägen, Products=8, Time=5}, Janne_3={Trip=Janne, Index=4, Lat=59.310587, Lon=18.027833, Name=Marievik, Products=8, Time=3}, Janne_4={Trip=Janne, Index=5, Lat=59.315665, Lon=18.034175, Name=Hornstull, Products=2, Time=3}}
ii=0, Map={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}
CurrentTrip=(MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}}
ii=1, Map={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}
CurrentTrip=(MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, 1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}}
ii=2, Map={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}
CurrentTrip=(MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, 1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}, 2={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}}
ii=3, Map={Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}
CurrentTrip=(MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, 1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}, 2={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}, 3={Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}}
ii=4, Map={Trip=Jobb, Index=5, Lat=59.331008, Lon=18.059293, Name=Stockholm City, Products=512, Time=3}
CurrentTrip=(MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, 1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}, 2={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}, 3={Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}, 4={Trip=Jobb, Index=5, Lat=59.331008, Lon=18.059293, Name=Stockholm City, Products=512, Time=3}}
Main_Create: Starter.CurrentTrip = (MyMap) {0={Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}, 1={Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}, 2={Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}, 3={Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}, 4={Trip=Jobb, Index=5, Lat=59.331008, Lon=18.059293, Name=Stockholm City, Products=512, Time=3}}
Skipped 49 frames!  The application may be doing too much work on its main thread.
*** Service (gpsservice) Create ***
GPSservice.Service_Create
** Service (gpsservice) Start **
GPSservice, GPSstart
Background partial concurrent mark sweep GC freed 28776(2MB) AllocSpace objects, 20(1496KB) LOS objects, 40% free, 19MB/33MB, paused 5.056ms total 59.977ms
GPSservice.Service_Start -12:01:12
LoadPoints=(MyMap) {Trip=Jobb, Index=1, Lat=59.280084, Lon=18.004647, Name=Älvsjögården, Products=8, Time=0}
LoadPoints=(MyMap) {Trip=Jobb, Index=2, Lat=59.278745, Lon=18.010347, Name=Älvsjö Station, Products=1, Time=3}
LoadPoints=(MyMap) {Trip=Jobb, Index=3, Lat=59.299231, Lon=18.030231, Name=Årstaberg, Products=1, Time=3}
LoadPoints=(MyMap) {Trip=Jobb, Index=4, Lat=59.313389, Lon=18.061405, Name=Stockholms Södra, Products=1, Time=3}
LoadPoints=(MyMap) {Trip=Jobb, Index=5, Lat=59.331008, Lon=18.059293, Name=Stockholm City, Products=512, Time=3}
Timeline: Activity_idle id: android.os.BinderProxy@e4f3518 time:1409825613
Local module descriptor class for com.google.android.gms.googlecertificates not found.
Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
Selected remote version of com.google.android.gms.googlecertificates, version >= 4
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000027/n/armeabi-v7a
ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000027/n/armeabi
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
This smacks of timing. Here is a pure guess - you have to be careful using Wait For in Activity_Create as it immediately returns from Activity_Create and then runs Activity_Resume, then later returns to complete Activity_Create. If you have not set up everything that Activity_Resume requires before calling Wait For then 'funny things' can happen.
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
This smacks of timing. Here is a pure guess - you have to be careful using Wait For in Activity_Create as it immediately returns from Activity_Create and then runs Activity_Resume, then later returns to complete Activity_Create. If you have not set up everything that Activity_Resume requires before calling Wait For then 'funny things' can happen.
Ok. Thanks.
I'll move it to Resume and see what happens.
 
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Well... it worked more or less.

I don't want to start the GPS-service in Resume since it will be started everytime Activity Main gets focus.
I want to start it once and then the service will take care of it self.
Then I need to add a flag to check if it's the first time or not, if yes then start service, if no, do nothing.
But the problem is if the GPS-service gets killed but not Starter then the flag won't work.
I could set the Flag False before GPS-service gets closed...
I need to work out how to handle it.

B4X:
'... code in Main, Activity Resume:
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result and Starter.FirstTime then
    StartService(GPSservice)   'Start GPS
End If

Questions about permission;
What is dependent on the permisson 'PERMISSION_ACCESS_FINE_LOCATION'? GPS and Mapfragment?
Can I start GPS and Mapfragment before I have checked Permission?
If not, will it be an error or will nothing happen?

I can't check for permission in Create but I want to start GPSservice in Create, so it's started only once.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Have you seen this example: https://www.b4x.com/android/forum/threads/gps-tutorial.6592/ ?
It correctly requests the permission. The GPS will only be started once.

Yes, thanks. Seen that but really don't know why I didn't use the code in Resume...
I will try that again.

But doesn't
B4X:
CallSubDelayed(Starter, "StartGPS")
in the tutorial you linked to, run everytime that Resume is run? Which means a lot of times...
The GPSenabled and Result will both be true every time.
B4X:
Sub Activity_Resume
   If Starter.GPS1.GPSEnabled = False Then
       ToastMessageShow("Please enable the GPS device.", True)
       StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
   Else
       Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
       Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
       If Result Then CallSubDelayed(Starter, "StartGPS")
   End If
End Sub

The reason I don't want this solution is that I am using 'StartServiceAt' with defined time for the GPS.
I can't normally have the GPS on all time, so I will enable it about every 5 minutes and when I get closer to target this will reduce time.
When very close the GPS will be on all time.

This is to save battery since I need it to be ready to start tracking when it gets closer to a stop-point, I need it to check the position regulary.
 
Upvote 0
Top