Android Question Service always live

fishwolf

Well-Known Member
Licensed User
Longtime User
I have suppose that a service is always live also when the app is closed.
Instead from result of my test i have see that is not true.

If i done a service that receive message from mqtt broken, i receive message only when the app is foreground or background, not when is closed.

Exist a mode for have the service always live?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User

i'm read and done some test, but don't find a solution.

this is a example for a service always running

B4X:
Sub Process_Globals
Dim EachMinut As Timer
Dim Counter As Int = 0

End Sub

Sub Service_Create

    ShowNotification("Service_Create", "FIRST")
   
    EachMinut.Initialize("EachMinut", 60000)
    EachMinut.Enabled = True
   
End Sub

Sub Service_Start (StartingIntent As Intent)

    Service.StopAutomaticForeground
   
End Sub

Sub Service_TaskRemoved
   
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub EachMinut_Tick
   
    Counter = Counter + 1
    ShowNotification("Timer", Counter)
   
End Sub

Sub ShowNotification (Title As String, Body As String)
Dim Notif As Notification

    Notif.Initialize
    Notif.Icon = "icon"
    Notif.Sound = True
    Notif.Vibrate = False
    Notif.Light = False
    Notif.OnGoingEvent=False
    Notif.SetInfo(Title, Body, Main)
    Notif.Notify(1)

End Sub
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
This code work fine on old Nexus S (android 4.3.1) , but doesn't work fine on ASUS ZenPhone 3 (androind 7)
 
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
There are many mistakes in the code you posted:

1. Don't use the starter service for this.
2. The service should be a foreground service.
3. You should acquire a partial wake lock with PhoneWakeState

Done, but Foreground call return error

Can you check example code or show me a valid example?

Thanks

B4X:
ror occurred on line: 22 (Scheduler)
java.lang.ClassCastException: anywheresoftware.b4a.objects.NotificationWrapper$NotificationData cannot be cast to android.app.Notification
    at b4a.example.scheduler._service_start(scheduler.java:220)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at b4a.example.scheduler.handleStart(scheduler.java:100)
    at b4a.example.scheduler.access$000(scheduler.java:8)
    at b4a.example.scheduler$1.run(scheduler.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
    at b4a.example.scheduler.onStartCommand(scheduler.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3414)
    at android.app.ActivityThread.-wrap21(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1632)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:208)
    at android.app.ActivityThread.main(ActivityThread.java:6267)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
 

Attachments

  • SERIVCE2.zip
    9.2 KB · Views: 210
Upvote 0

fishwolf

Well-Known Member
Licensed User
Longtime User
This code is wrong:
B4X:
    Notif.Initialize
   Service.StartForeground(1,Notif)
You can only use the notification after you call SetInfo.

OK, now work fine,

but is linked to first notification with app in foregroud.
example:
Success: Run app, start automatic first notification, close app, i can receive new notifications from mqtt prodocol.
Doesnt'Work: Run app, close app, I CANNOT RECEIVE NEW NOTIFICATIONS.

can you explain why the startforegroud is linked to notification and permit to service to run always?

thanks
 
Upvote 0
Top