Android Question PhoneSensors Stop Listening

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I'm trying to stop the PhoneSensors from listening but am having trouble doing it. Can you look at my code and let me know what I still need to do?

This these next 3 sub routines have coding that supposedly stops the listening.

B4X:
Sub Activity_Resume

    DisplaySalatTimes

    TheOrientation.StopListening
    TheSensors.StopListening

    PanelActivateQiblaLocator.Visible = True
    PanelTurnRight.Visible = False
    PanelTurnLeft.Visible = False
    PanelCalibrateCompass.Visible = False

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    TheOrientation.StopListening
    TheSensors.StopListening
End Sub

B4X:
Sub CustomViewExit_Click()

    bm.Initialize(LoadBitmap(File.DirAssets,"Exit Pressed.png"))
    CustomViewExit.setIcon(False,bm)
    
    TheOrientation.StopListening
    TheSensors.StopListening

    jo.InitializeContext
    jo.RunMethod("finishAffinity", Null)
End Sub

This coding starts the orientation listening if the compass does not need calibration.

B4X:
Sub TheSensors_SensorChanged (Values() As Float)

    If TheSensors.Accuracy <> 3 Then

        PanelCalibrateCompass.Visible = True
       
        bm.Initialize(LoadBitmap(File.DirAssets,"Exit.png"))
        IconButtonExitCalibrate.BackgroundColor = Colors.Transparent
        IconButtonExitCalibrate.setIcon(False,bm)
    Else
        PanelCalibrateCompass.Visible = False
        TheOrientation.StartListening("TheOrientation")
    End If
End Sub

This sub routine keeps executing after I exit the app and load it up again. It is supposed to get activated only when the user taps an Image button. The sub routine for the Image button is the next section of code after this one.

B4X:
Sub TheOrientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)

    PanelKaaba.Visible = False
    PanelTurnLeft.Visible = False
    PanelTurnRight.Visible = False
    PanelCalibrateCompass.Visible = False

    If (NumberFormat( Azimuth, 3, 0) >= (Starter.intQiblaHeading) -3) And _
        (NumberFormat( Azimuth, 3, 0) <= (Starter.intQiblaHeading) +3) Then
       
        PanelKaaba.Visible = True
    Else
        If (NumberFormat( Azimuth, 3, 0) >= (Starter.intQiblaHeading) -3) Then
            PanelTurnLeft.Visible = True
        Else
            PanelTurnRight.Visible = True
        End If
    End If
End Sub

This is the coding for the Image button that starts the sensor to listen.

B4X:
Sub ImageViewActivateQiblaLocator_Click
   
    PanelActivateQiblaLocator.Visible = False
   
    TheSensors.Initialize(TheSensors.TYPE_ORIENTATION)
    TheSensors.StartListening("TheSensors")
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I put in a temporary work-around. It's the outer "If" statement. If the "Locate" image button is not displayed then the coding for the orientation will execute otherwise it won't because that "Locate" button will be showing each time the app is loaded up.

B4X:
Sub TheOrientation_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)

    PanelKaaba.Visible = False
    PanelTurnLeft.Visible = False
    PanelTurnRight.Visible = False
    PanelCalibrateCompass.Visible = False

    If PanelActivateQiblaLocator.Visible = False Then
        If (NumberFormat( Azimuth, 3, 0) >= (Starter.intQiblaHeading) -3) And _
            (NumberFormat( Azimuth, 3, 0) <= (Starter.intQiblaHeading) +3) Then
            
            PanelKaaba.Visible = True
        Else
            If (NumberFormat( Azimuth, 3, 0) >= (Starter.intQiblaHeading) -3) Then
                PanelTurnLeft.Visible = True
            Else
                PanelTurnRight.Visible = True
            End If
        End If
    End If
End Sub
 
Upvote 0
Top