Android Question About services live time

AndrewChan0513

Member
Licensed User
Longtime User
1,
My services always auto stoped, but there is no error,
I've try to set the android's "Background process limit".
When I set "Standard limit", then my services will stop after 2~10 mins.
When I set "At most 4 processes", then my services will stop after 1 sec = =!

"Standard limit" (No any error, just need to start the services again after it stoped):

How can I make my services keep alive?

That "s2" service code :
B4X:
Sub Service_Create
    Timer1.Initialize("Timer1",1000)
    Timer1.Interval = 1000
       Timer1.Enabled = True
End Sub


Sub Timer1_Tick
    Dim Time_Num As Int
    DateTime.DateFormat="HHmmss"
    Time_Num = DateTime.Date(DateTime.now)
        DateTime.DateFormat="HH:mm:ss"
    If ((Time_Num > 75900) AND (Time_Num < 200000)) Then
        n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "Start!", Main)
        Timer1.Enabled = False
        Timer2.Initialize("Timer2",2500)
        Timer2.Interval = 1500
           Timer2.Enabled = True
        TimerN = 0
        t = 0
    Else
        n.SetInfo("[" & DateTime.Date(DateTime.now) & "]", "It will start at 07:59", Main) 'Change Main (above) to "" if this code is in the main module.
        n.Notify(1)
    End If

End Sub

2, About GCM problem
After a long time, when I use gcm send something to my device, it got a error msg.
Is that gcm have a live time too ?
Or just I do something worng...

3, I've write a function in GCM service ( MessageArrived ) to call a service.
B4X:
    If Intent.HasExtra("data") Then TypeN = Intent.GetExtra("type")
        If TypeN = "3" Then
            StopService(s1)
            StartService(s2)
            CallSubDelayed(s2,"ReStart")
            ToastMessageShow("Start Services", True)
            Return
        Else If TypeN = "4" Then
            ToastMessageShow("Kill Services", True)
            s2.Timer2.Enabled = False
            StopService(s2)
            StopService(s1)
            Return
        End If
In type = 3,
The service is start properly,
But in type = 4,
The service will restart sometimes, and sometimes can stop properly. -_-!!!
Log :
1, type 3 -> It started.
2, type 4 -> It stoped.
3, type 3 -> It started.
4, type 4 -> It stoped.
5, type 3 -> It started.
6, type 4 -> It restarted !!!!!!
7, type 4 -> It restarted !!!!!!
8, type 4 -> It restarted !!!!!!
9, type 4 -> Finally, It know stoped.


4, Fromula problem

4.1, I dim a (NBnum as Int) in (Process_Globals), then I make it when messagearrived one time, it will add 1 it self.
But it didn't, is it because everytimes when GCM coming it will start the services again, so the NBnum will always keep at 10 ?
If is that, can I temp a var in GCM services? or do I need to put NBnum = 10 in (Process_Globals) ? Is there is the right way to do that?

4.2,
"If Line1.Length > 0 Then" this not working - -!
And I've try "If Line1 = "" Then",
is also not working.
It don't care Line1 this var have / don't have value,
And it will do inside if : "nbInboxStyle.AddLine = Line1"

B4X:
Sub Service_Start (StartingIntent As Intent)
    NBnum = 10
End Sub

Sub MessageArrived (Intent As Intent)
   If TypeN = "2" Then
        NBnum = NBnum + 1
        nb.OnGoingEvent = False
        nbInboxStyle.BigContentTitle = Title
        nbInboxStyle.SummaryText = Summary
        If Line1.Length > 0 Then
            nbInboxStyle.AddLine = Line1
        End If
        If Line2.Length > 0 Then
            nbInboxStyle.AddLine = Line2
        End If
        If Line3.Length > 0 Then
            nbInboxStyle.AddLine = Line3
        End If
        If Line4.Length > 0 Then
            nbInboxStyle.AddLine = Line4
        End If
        If Line5.Length > 0 Then
            nbInboxStyle.AddLine = Line5
        End If
        nb.SetStyle(nbInboxStyle)
           nb.Notify(NBnum)
    End If
End Sub

5, Mainfest Problem
I've using Receive SMS method
B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(s1,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
AddReceiverText(s2,
<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>)
Can I have a solution :
If s2 is active, just s1 have action.
If s1 is active, just s2 have action.
Doing like this ?

P.S. I attatched my full app source code.
 

Attachments

  • chbv8.zip
    31.6 KB · Views: 188
Last edited:

AndrewChan0513

Member
Licensed User
Longtime User
Have you seen this?
I am very appreciative of your help.
I've already google 3 days..
But I can't find out this method.
May be I am using the wrong keywords ..
I found a lot of resources is saying use "onCreate" to create services - -!
You help me to fix my very big trouble...

Thanks a lot again..
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
1, I am using this code to fixed
B4X:
#StartCommandReturnValue: android.app.Service.START_STICKY

2, Wait for tomorrow to try again

4.1, I've tried put the NBnum to global, it seems work. Seems no error.
4.2, I've using this code to fixed
B4X:
If Line1 <> Null Then
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
Oh. No..
I've already use START_STICKY.
But the service still get kill by android after 3hrs - -!
Help - -!!

And sometimes it show an error msg :


Attached my new source code
 

Attachments

  • chbv8.zip
    32 KB · Views: 198
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
I am sorry as I had no opportunity to read the code. Have you tried to use the PhoneWakeState :http://www.b4x.com/android/help/phone.html#phonewakestate

If yes, I have learned by experience that some lockscreens put the device to sleep even if we have put a different State. If possible try to unable the lock screen to see if it is better. And if it is the issue, you'll find on the forum the necessary code and samples to make your own lock screen.

Hope this could help
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
The problem is in your Process_Globals sub. You should only use it to declare variables.
I don't really understand..
"Unable to create service com.andrewchan.smsforbotv2.pushservice:"
In pushservice,
B4X:
Sub Process_Globals
    Dim NBnum As Int
    NBnum = 10
    Dim nb As NotificationBuilder
    Dim nbBigTextStyle As NotificationBigTextStyle
    Dim nbInboxStyle As NotificationInboxStyle
End Sub
It just declare variables..
There is no any else in Process Globals = =
But it still have error sometimes.. Not everytimes - -

And the main problem is..
Many times is no any error..
But the services still auto killed..

I've tried 4 devices..
1. Samsung Note 2 LTE
2. Samsung S4
3. Samsung S5
4. THL A1

Just Samsung S5 can no error and won't stop service - -!

What should I do ..... - -!!
Help~



phonewakestate will make the phone screen on - -!
My client will kill me if I did that = =!

Anyway.. Thanks for you help
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
No. You declare it (Dim) and you assign an Value to it
B4X:
NBnum = 10
should not be used in process global.
Move it to Activity create or resume

Many thanks, take it away now, then no that error now..

See the error message. It happens in Main.Process_Globals where you mistakenly initialize StringFunctions.

Now have a new error message -_-!!!

Object should first be initialized (Intent)
is it talking about :
B4X:
Sub MessageArrived (Intent As Intent)

But if yes... what can I do - -???
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
Here is the newest error log and the newest source code

But without this error.
"S2" Service have 2 problem too = =!!!!
1, It will auto stop and it will auto start again.. But not work properly...
2, It will auto stop.. And no next !!!!!
I am taking too long to trying to fix this problem = =!
But what I can do.. I've already tryed. And it still will auto stop !!!!!!

B4X:
Sub Service_Create
    If S2Enable <> 1 Then
        StopService(Me)
    End If
    sf.Initialize
    username = StateManager.GetSetting2("user","")
    password = StateManager.GetSetting2("pass","")
    pc = StateManager.GetSetting2("pc","0") + 1
    phonenum = StateManager.GetSetting2("num","64500366")
       DateTime.DateFormat="HH:mm:ss"
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("[" & DateTime.Date(DateTime.now) & "]Started", username & "," & password & "," & pc & "," & phonenum, Main)
    n.Notify(0) 'Auto Restart to Here !!
    n.Vibrate = False
    n.Sound = False
    Timer1.Initialize("Timer1",1000)
    Timer1.Interval = 1000
       Timer1.Enabled = True 'Timer didn't work after it auto restart
 
    Timer2.Initialize("Timer2",1500)
End Sub

 

Attachments

  • chbv10.zip
    32.5 KB · Views: 183
Last edited:
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
I've added a lot of LOG to check what step it's doing..


The problem is in here :

The timer is starting from 20:21:09 to 20:21:44 !!
But 20:21:45 to 20:21:49 what it is doing - -? Disappeared ??
20:21:50 Why the service will suddenly Create again?

20:21:50.244 I/B4A (15705): S2 Timer1 Enabled
It have already enabled the timer1
But after Service start.. Timer1 is disappeared !!


Help = =!!
 
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
Why aren't you reading the logs in the IDE?

Can you post the code in Service_Start?

In IDE is mean in the Basic4android right hand side's Log ?
- -! Is not the same with B4A Log Viewer ?

B4X:
Sub Service_Start(startingIntent As Intent)
Log("S2 Service Start" & " -- S1Enable : " & s1.S1Enable & " S2Enable : " & S2Enable)
End Sub

There is nothing in Service_Start = =

The main point is...
Why the service(s2) will got killed ? by who ? Itself ? Android ? But I've already set sticky!
 
Last edited:
Upvote 0

AndrewChan0513

Member
Licensed User
Longtime User
I found it !!
http://developer.android.com/reference/android/app/Service.html

intSTART_REDELIVER_INTENTConstant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).
intSTART_STICKYConstant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent.

Is there have a way I can use START_REDELIVER_INTENT in basic4android?

I don't know why the service got killed.
But START_STICKY will help me to restart it again..
But the intent didn't return to it again - -.
So I got a error "Object should first be initialized (Intent)."

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can see that there was a crash in Service_Start in a call to Intent.Action:

20:21:50.264 I/B4A (15705): ~e: at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
20:21:50.264 I/B4A (15705): ~e: at anywheresoftware.b4a.objects.IntentWrapper.getAction(IntentWrapper.java:56)
20:21:50.264 I/B4A (15705): ~e: at com.andrewchan.smsforbotv2.pushservice._service_start(pushservice.java:462)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…