Android Question Close Android Built-In Application

EvgenyB4A

Active Member
Licensed User
Longtime User
If I open any Android built-in application like Settings or Browser by Staring Intent from my app, can I close these apps from my app?
 

JordiCP

Expert
Licensed User
Longtime User
Usually these intents return back to the activity when the user presses the back button after changing whatever settings.

You cannot close it directly from your activity, since it will be paused, but you can always bring back your activity to foreground with a simple trick
Start a service with a timer before launching the intent and when the timer expires, it will bring back your activity to foreground

Your activity
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim myButton As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   myButton.Initialize("myButton")
   myButton.Text="Launch intent"
   Activity.AddView(myButton,20dip,50%Y-40dip,100%X-40dip,80dip)
End Sub

Sub startingIntent
  
   StartService(myService)
   Dim i As Intent
   i.Initialize("", "")
   i.SetComponent("com.android.settings/.Settings")
   StartActivity(i)
End Sub

Sub myButton_Click
   CallSubDelayed(Me,"startingIntent")
End Sub
Add a service caller "myService"
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim t As Timer
   Dim sNotif As Notification
End Sub

Sub Service_Create
   'Notif needed for foreground service
   sNotif.Initialize
   sNotif.Icon="ic"
   sNotif.SetInfo("Hello", "Hello", Main)
   sNotif.Vibrate=False
   sNotif.Sound=False
   Service.StartForeground(1, sNotif)

   t.Initialize("t",5000)
End Sub

Sub Service_Start (StartingIntent As Intent)
   t.Enabled=True
End Sub

Sub t_tick
   t.Enabled=False
   If IsPaused(Main) Then
     Log("Activity is paused. Starting...")
     StartActivity(Main)
   Else
     Log("Activity is already active")
   End If
  
   StopService(Me)
End Sub
 
Upvote 0
Top