Android Question Detect Location Settings Changes

fbritop

Active Member
Licensed User
Longtime User
Is there a way to detect when a user has enabled or disabled the location service from the phone settings?.

I know I can always check for "location_providers_allowed". The problem is that I cannot detect when a user has change it.

Thanks
FBP
 

fbritop

Active Member
Licensed User
Longtime User
Erel,
The problem goes beyond that. Because if the user disconnects or connects the location settings directly from the settings that are displayed when he swipes his finger from the top, then Activity_Pause or Activity_Resume are never called.
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Thanks Erel,
At first it did gave an error exception on some circumstances. It made me realize that this sub should be written after the Activity_Create.

Thanks again
FBP
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Sory, but after some tests there is still an execption in the whole SUB, I cannot parse it into a try catch exception becasuse it is by a general sub and not within the SUB.

B4X:
MAC:amain
** Activity (amain) Pause, UserClosed = false **
MAC:amain
sending message to waiting queue (CallSubDelayed - getDB)
** Service (servicelocation) Destroy **
Activity_WindowFocusChanged:false
amain_activity_windowfocuschanged (java line: 1068)
java.lang.NullPointerException: Attempt to read from field 'cl.abreme.android.x cl.abreme.android.amain._vvvvvvvvvvvvvvvvvvvvvvvv7' on a null object reference
    at cl.abreme.android.amain._activity_windowfocuschanged(amain.java:1068)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at cl.abreme.android.amain.onWindowFocusChanged(amain.java:183)
    at com.android.internal.policy.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:3759)
    at android.view.View.dispatchWindowFocusChanged(View.java:10242)
    at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:1216)
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4186)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
Upvote 0

xpectmore

Member
Licensed User
Longtime User
yes you can detect if the user disable location very simple

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    olat=Location1.ConvertToMinutes(Location1.Latitude)
    olon=Location1.ConvertToMinutes(Location1.Longitude)
    lblLat.Text = "Lat = " & olat
    lblLon.Text = "Lon = " & olon
    Compute_date_time(Location1.Time / 1000)

End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub GPS_GpsStatus (Satellites As List)
check
    lblSatellites.Text = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
            & " " & Satellite.Elevation
    Next
End Sub


Sub GPS_NMEA (timestamp As Long, Sentence As String)
End Sub

Sub check
    If gps1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(gps1.LocationSettingsIntent) 'Will open the relevant settings screen.
        Else
        gps1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

so you see the attach gps_to_php.zip
there you can find b4a part and php part witch you can test it quick in windows xampp server
if you are php expert you know what comd is and you must create first data.txt to cmode it..

you can see the results like http://your_website_you_upload_the_script.com/data.txt and you may refresh it in browser any time on android device you see "send"
in put.php you can also develop insert in a mysql database ... and then an index.php file wil manage the view with some jquery

on xampp create in htdocs an folder : eg.. demo
start the server
in browser put http://localhost/demo/data.txt

and you will see all data received from your gps track
 

Attachments

  • gps_to_php.zip
    9 KB · Views: 339
Last edited:
Upvote 0

xpectmore

Member
Licensed User
Longtime User
yes it is ,you are wrong

question:
Is there a way to detect when a user has enabled or disabled the location service from the phone settings?.

I know I can always check for "location_providers_allowed". The problem is that I cannot detect when a user has change it.

Thanks
FBP

answer

B4X:
Sub GPS_GpsStatus (Satellites AsList)
check
lblSatellites.Text = "Satellites:" & CRLFFor i = 0To Satellites.Size - 1Dim Satellite AsGPSSatellite
Satellite = Satellites.Get(i)
lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _" " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
& " " & Satellite.ElevationNext
End Sub

Sub check
    If gps1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(gps1.LocationSettingsIntent) 'Will open the relevant settings screen.
        Else
        gps1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub
^---this working like an trigger.if the user will open the apk and will first activate location and with open app will close location the app will popup the possibility to activate again location cause the app needed.so this is the way to detect if user had close=deactivate location(
Is there a way to detect when a user has enabled or disabled the location service from the phone settings?.
)
that is the answer Erel,with your aknolege in this language is strange you miss the point.and bumping old posts links our experience from old to new is equal to they call it LEARNING ... manny mistakes Erel :)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
that is the answer Erel,with your aknolege in this language is strange you miss the point.and bumping old posts links our experience from old to new is equal to they call it LEARNING ... manny mistakes Erel
There is no place for such remarks in this forum. Next time they will be deleted.
Also don't bump old threads.

The question asked was how to know when the setting has changed, not how to check the current state.
It also has nothing to do with any external server.
 
Upvote 0
Top