Android Question Is it possible to stop service started by other app?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have 2 service app, App A & App B.

App A is just a simple app, no UI and has a service to display a notification on a certain time, for ex 1 hours.

App B is an app to set time for App A to display this notification.

On App A, the code to start a service is like this
B4X:
Sub Service_Start (StartingIntent As Intent)
    If KV.IsInitialized = False Then KV.Initialize(File.DirInternal, "KV") ' KV is a KeyValueStore
    StartServiceAt(  Me, DateTime.Now + KV.GetObject("RefreshTime"), True)
End Sub

On App B, I want to change RefreshTime by changing the value in KV.
The idea are :
- Stop Service (started by App A)
- change RefreshTime value
- restart service again

I tried StopService("") and CancelScheduledService("") on App B, but no avail.
Is it possible to stop service started by App A?
 

DonManfred

Expert
Licensed User
Longtime User
You should put both together in one app. App A has no gui, app B has one. So; instead of using the ui in app b you should use app a to start and configure your service. Also UI to change time.

You can not stop a service from App B started by app A. Except your device is rooted it should be possible but i don´t know HOW
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I made a test for using intent, beside, also needed to able to stop a service. This is my codes on the App that has a Service module.
B4X:
Sub Activity_Resume
    Dim In As Intent
     In = Activity.GetStartingIntent
     If In <> Null Then
        If In.HasExtra("StopService") Then
            StopService(myService)
            CancelScheduledService(myService)
            Dim PUB_Nt As Notification
            PUB_Nt.Initialize
            PUB_Nt.Icon = "icon"
            PUB_Nt.Light = True
            PUB_Nt.Vibrate = False
            PUB_Nt.AutoCancel = True
            PUB_Nt.SetInfo("Info","Listening Service Stopped!", "")
            PUB_Nt.Notify(1)
        End If
        Dim returnIntent As Intent
        returnIntent.Initialize(In.GetExtra("Callback"),"")
        returnIntent.AddCategory("android.intent.category.DEFAULT")
        StartActivity(returnIntent)          
     End If
End Sub

Just tried to stop the service, notification show up, but service still running.

Did I miss something?
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
I have tried it, but it didn't work, notification shows up but service still running.
Here is the manifest file in the app that has service
B4X:
AddActivityText(Main, <intent-filter>
   <action android:name="com.myAppName.REQUEST" />
   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>)
SetServiceAttribute(myService, android:exported, "true")
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

I add a Cancel button for a test
B4X:
Sub btnCancel_Click
    StopService("")
    CancelScheduledService("")
    'ExitApplication  
End Sub

Service still running after btnCancel Clicked. Only if comment on ExitApplication removed, service stopped. Could it be this the reason? No possible to stop running service without ExitApplication?

And where is the bug that cause calling intent multiple times?
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Sorry for late reply, been away a while from PC.
Found the problem, my mistake, put the code to start service like this
B4X:
Sub Activity_Create(FirstTime As Boolean)
  StartService(myService)
  Activity.Finish
End Sub
Change the code to this
B4X:
Sub Activity_Create(FirstTime As Boolean)
  If FirstTime Then
     StartService(myService)
  End If
  Activity.Finish
End Sub
Don't know if is that the right solution, but seem worked just expected.
Now the next issue
I change codes that cause a bug into like this
B4X:
Sub Activity_Resume
  Dim In As Intent
  In = Activity.GetStartingIntent
  If In <> Null Then
  If In.HasExtra("StopService") Then
  StopService(myService)
  CancelScheduledService(myService)
  
  Dim PUB_Nt As Notification
  PUB_Nt.Initialize
  PUB_Nt.Icon = "icon"
  PUB_Nt.Light = True
  PUB_Nt.Vibrate = False
  PUB_Nt.AutoCancel = True
  PUB_Nt.SetInfo("Info","Listening Service Stopped!", "")
  PUB_Nt.Notify(1)
  Dim returnIntent As Intent
  returnIntent.Initialize(In.GetExtra("Callback"),"")
  returnIntent.AddCategory("android.intent.category.DEFAULT")
  returnIntent.PutExtra("Successful","True")
  StartActivity(returnIntent)  
  End If
  End If
End Sub
Is that the right correction? With those codes, I didn't see any error in the log window.

I made this code to stop running service in other app
B4X:
Dim In As Intent
   In.Initialize("gsf.AppthatHasService.REQUEST","")
   In.AddCategory("android.intent.category.DEFAULT")
   In.PutExtra("Callback","gsf.ApptoStopService.CALLBACK")
   In.PutExtra("StopService","True")
   StartActivity(In)

When those codes called, screen blink for a while, notification shows up, tell service has stopped, and then back to the caller app.
But when I clicked notification icon, my caller app disappear, just like users pressed Back /Home button. Is this related to bug you said before?
 
Upvote 0
Top