Android Question Application does not close

chrjak

Active Member
Licensed User
Longtime User
Hey Community,

I have a GPS Sensor and I want that it becomes closed when you click on its notification. I've done that with a separate Activity called "StopGps", that only kills the service when it gets started and kills itself immediately after. That worked not very well but it worked often.

Now I changed it to the Main Module and I am using that Code before all other lines in Activity_Create and Activity_Resume:
B4X:
Dim StartingIntent As Intent = Activity.GetStartingIntent
If StartingIntent.HasExtra("Notification_Tag") Then
        If StartingIntent.GetExtra("Notification_Tag") = "StopGPS" Then
            StopService(GPS)
            GPSStopped ' A sub thats testing if the user had open the app when he clicked on notification and if its yes it saves the last values - It's not important to know for my StopService problem: It only writes some personal App settings into a file
            ExitApplication
            Activity.Finish   
End If
    End If

So in fact there are two killing code lines before all other Activity_Create Code and in _Resume another time... but somehow that does not work. When i click on the notification while the app is paused it opens the app after stopping the service nevertheless...

Sadly i am not allowed to publish the project...
But could you please help me anyway? I am also interested if there is a more easy way to kill the Service when clicking on its notification

Thank you so much
 
Last edited:

chrjak

Active Member
Licensed User
Longtime User
I noticed that this solution is not the best either

When you stop the GPS and the App still is started (The App should not always stop when you click the notification - Sometimes it just changes the Activity when the app is opened when the user clicks on the notification) and you change the activity. then you come back to the main and the startingintent (which is still the kill intent) is recalled in _resume... So ignore the above:

The new question is:

How can I stop the GPS Service through a Notification?

Please notice that an event has to be called in the StoppingProcess which might not close the App. (As above it sometimes only changes the activity but lets the app still open)
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
Hey Thanks for your reply

I am already doing that (but i am not sure if i understand you completely - I can't enter a Service into the Notification Module (n.SetInfo("GPS", "Stop GPS", ->module<-)) Could you please give me an example to show what you mean? Thanks)

B4X:
Sub Service_Start (StartingIntent As Intent)
    ServiceActive = True 'Not important Boolean
    GPS1.Start(0, 0)   
    n.Initialize
    n.Icon = "ic_gps_not_fixed_white_48dp"
    n.Vibrate = False
    n.Light = False
    n.Sound = False
    n.OnGoingEvent = True
    n.SetInfo("GPS", "Stop GPS", stopgps)
    Service.StartForeground(5,n)
End Sub

Sub Service_Destroy
    GPS1.Stop
    ServiceActive = False
    Service.StopForeground(5)
End Sub
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
You're right. That works better:
B4X:
Sub Service_Create
    GPS1.Initialize("GPS1")
    If GPS1.GPSEnabled = False Then
        StartActivity(GPS1.LocationSettingsIntent)
    Else
        GPS1.Start(0, 0)
        ServiceActive = True
        n.Initialize
        n.Icon = "ic_gps_not_fixed_white_48dp"
        n.Vibrate = False
        n.Light = False
        n.Sound = False
        n.OnGoingEvent = True
        n.SetInfo("A", "B", stopgps)
        Service.StartForeground(5,n)
    End If   
End Sub

Sub Service_Start (StartingIntent As Intent)
    
End Sub

Sub Service_Destroy
    GPS1.Stop
    ServiceActive = False
    Service.StopForeground(5)
End Sub

But that still doesn't solve the problem, that the app doesn't listen to the exit commands. When the app is minimized using the home button and the user clicks on the notification the app starts and stops the gps - but it restarts it immediately because the activity does not close (Or why has the main activity to start at all? When i call the stopgps module)

Is there maybe a better way?
And if the user has the app open everything works fine...
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
It's empty

I have the old solution:
StopGPS:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
    StopService(GPS)

    If Values.PhoneHasSensor = True Then
        CallSubDelayed(Main, "ImageView4_Click")
    Else
        ExitApplication
    End If
    End If

    Activity.Finish   
End Sub
The Rest of this Module is empty

For the service code look above
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
If you don't stop all sensors, timers, services.. when you call ExitApplication the app is restarted.
The normal way to close your app is to stop anything, close your activities and let the system delete the process when it needs.
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
Thanks. Good to know

i've stopped every timer, every sensor and every service now. then i call exitapplication and before that activity.finish but the app gets started anyways!?

B4X:
StopService(Starter)
        StopService(GPS)
        StopService(TrackService)
        TrackService.PS.StopListening
        Main.ps.StopListening
        Main.clicktimer.Enabled = False
        Main.waitingtimer.Enabled = False
        TrackService.Timer1.Enabled = False
        Activity.Finish
        ExitApplication
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
I'm unsure about how to do that. Can we close activities by remote(/other modules)?
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
I tried it again but i can't figure it out :/ When i click the notification i come to my activity that stops all services and kills itself at the end. Nevertheless I become redirected to the main module. Is that because the Starter Service maybe starts after the module has killed itself so that the killcommand for this service doesn't take effect? I can't figure it out :/

Or is there maybe another way to stop a service by a notification click?

I admire your help. Thank you very much
Merry Christmas
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
No i have taken this out. Instead of this i kill every service, sensor,... but the main activity i can't stop by the 'stopgps' module. So i created a kill boolean, which kills the main in activity_resume (Main) if it's true. the problem is, that sometimes on restarting the app it somehow stops itself because the kill is true. but i set it to false before i finish the activity in _resume. Little bit crazy
 
Upvote 0

chrjak

Active Member
Licensed User
Longtime User
This is a good idea! I'll try it out.

Is 500ms enough time for the system? I don't know if there are some very old phones, that need more than a half second to close activitys
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…