Android Question Starting service after the process is killed

hookshy

Well-Known Member
Licensed User
Longtime User
If I enter the aplications in my phones and stop the process my services not start anymore ....
My service is scheduled to restart with
StartServiceAt("", DateTime.Now + DateTime.TicksPerSecond*120 ,True)
service_destroy is not called in this case
Service_destroy is called only if I go to running apps , track the service and hit stop .

Another issue is:
If I have boolean values that are defined as process globals in main activity and saved to disk , when my process starts I have to read the variables from disk in service_start sub in order to read the correct state of the variable
I have fixed the problems and in the future I will start developing from Service Starter with b4a v5.20

Q: how can I manage a reliable service module for critical operation like : receiving sms ...if the process is killed (from installed app not from running apps ) then the service may not start at all , or if it if the service is killed then can I start the service right away in service_destroy ?
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
how can I manage a reliable service module for critical operation like : receiving sms
By registering an intent receiver in the Manifest, perhaps ?
if the process is killed (from installed app not from running apps ) then the service may not start at all , or if it if the service is killed then can I start the service right away in service_destroy
By installing a second app sending an itent on regular schedules to the service which has to be running, perhaps ?
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
That's right. This is how Android works. If the user explicitly kills your app then there is nothing that you can do to start the process again. Only the user can start it again.

Thanks , this seems resonable since it depend only on user action ...and most of the task killers does not stop the process but closes all activities and services
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
By registering an intent receiver in the Manifest, perhaps ?

I do not know if there is any diference from this solution
Intent Filters - Intercepting SMS messages in the background
and
Phone library with sms interceptor (this solution I am using in my app)
I guess the phone library depends on a service running , is the service started automaticaly when the sms arrives ?


 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
I have a device huawei ascend y530 with jelly bean
Pressing long on menu will start build in task killer , the service_destroy is not called in this case ?
problem is that I do not understand what this killer does to the app ..process globals from main activity are reinitialised to default ...meaning that the process is killed ...and it includes the services also ...do not know


If I enter running services from app settings then the service_destroyed is called after pressing stop service
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
is the service started automaticaly when the sms arrives ?
Yes when you register the intent receiver from the Manifest, the service is started automatically (or restarted if already running)
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
This is no longer true. If the user swipes your app from the recent apps list then it will be killed and will not start until the user starts it again.

yes I have tested this also on another device , is this the reason you added Startup Service ? ...
I have changed recently to intercepting sms in the backgbound and registered the internt sms receive ...but it seems I have a problem with the sms notification first time after i swipe the recent items and close the process the sms notification that was supposed to show up when the sms service starts does not show up ... The second sms wakes up the process ...I will double ceck this and come back
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
is this the reason you added Startup Service ?
No, it is not related to the starter service.

but it seems I have a problem with the sms notification first time after i swipe the recent items and close the process the sms notification that was supposed to show up when the sms service starts does not show up
This is exactly the result of this new security / protection feature. Once the user killed your app there is nothing that can be done to start the process without the user intervention.
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
This is exactly the result of this new security / protection feature. Once the user killed your app there is nothing that can be done to start the process without the user intervention.

I understood first time when the users killed the app from installed app ...but swiping the recent apps allows the processes to start again when skeduled with startserviceAt ...and in particular if the user sends the second sms the service wakes up ...so the process is still waken by the sms received intent
 
Upvote 0

xpectmore

Member
Licensed User
Longtime User
I know is later ... but still.... looking to the answer to your problem(cause i am still b4a beginner,java 0 =i'll never learn java) .. i found the answer ,i mean Erel did in da past .thanks Erel for https://www.b4x.com/android/forum/t...-long-running-background-tasks.27065/#content still works!
is about #StartCommandReturnValue: android.app.Service.START_STICKY

here is a small example will set allways vibrations instread sounds on your andoid phone.
(on my phone with android 6.0.1 if i set vibrations instread sounds after any call the phone app will set sounds overriding my settings so treat this experiment like a fix for my phone with android version)

i compiling this using android targetSdkVersion="19" on an android 6.0.1 and works !


the libraries are core and phone ...see on the right side-bottom da book then you may activate phone library(for begginers ninja )
in the main
B4X:
#Region  Project Attributes 
	#ApplicationLabel: CUSTA-mizable
	#VersionCode: 1
	#VersionName: 0
	'SupportedOrientations possible values: unspecified, landscape or portrait.
	#SupportedOrientations: unspecified
	#CanInstallToExternalStorage: True
#End Region

#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: False
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Activity.Finish
	
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")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

in the service starter i use

B4X:
#Region  Service Attributes 
	#StartAtBoot: True
	#ExcludeFromLibrary: True
	#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
Dim p As Phone

Dim vl As Int
Dim timer1 As Timer
End Sub

Sub Service_Create
	'This is the program entry point.
	'This is a good place to load resources that are not specific to a single activity.

End Sub

Sub Service_Start (StartingIntent As Intent)

timer1.Initialize("Timer1", 500)
timer1.Enabled = True
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Return True
End Sub

Sub Service_Destroy

End Sub


Sub Timer1_Tick

'If p.GetVolume(p.VOLUME_RING)>0 Then
'	p.SetVolume(p.VOLUME_RING, 0, True)
'	pp=False
'End If
	'delay
'
vl = p.GetVolume(p.VOLUME_RING)
If vl>0 Then
'Log("da")
p.SetVolume(p.VOLUME_RING, 0, True)
Else
'Log("nu")	

End If	
'Log(vl)
End Sub


how this work ? if pushing volume in android 6.0.1 will popup da bar then touching quick da arrow on da right of percents of voume then will popup a new small window ,then if you try to set ringtone to other values >0 the service will put it to 0 that's meaning "to vibrate".if you try to kill my app from recent apps this will kill activity and the service will live in backgroung.the unique way to kill it is settings / applications/application manager/the name you choose to try this in my case is CUSTA-misable\and Force stop and when popup confirmation force stop again.
the seccond way is simple as uninstall from the same place.[/code]
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Please use
B4X:
code here...
tags when posting code.
If I had a penny for each time @Erel uses the above sentence in his response, I would accumulate a fortune.@Erel: Is there a way the 'Questions' forum script can detect when someone posts code and neglects to use code tags, especially when the code is lengthy, and refuse to post until tagged. Sorry for posting this in the wrong forum, but it gets more exposure here..
 
Upvote 0

xpectmore

Member
Licensed User
Longtime User
isn't our fault if your website script is incomplete ... how i can know your tags are active , there is no relate to this when we comment.
maybe is time you write some code to the website too .. i am customer to purebasic too if you need suggestions about proffesional forum for real programmers go to http://www.purebasic.fr/english/ and you'll see any coment you do is showing first and people will know when someone coment. that's purebasic progress so why shouldn't be the same with b4a ?
chop chop ...
 
Last edited:
Upvote 0

xpectmore

Member
Licensed User
Longtime User
Cheappy is a savage rat
That can handle a red hat :
- "chop chop ... i still got..
the right answer is his cheecky hot .. "

he he he he he he he he he he
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…