Android Question Destroyed service, never starts again

nicolino33

Active Member
Licensed User
Longtime User
Hello all, in some huawei smartphone with android 6.0 B4A services are always stopped as soon as you close the app, while those like whatsapp, lovoo, and other chats remain active, why? What should I insert in the manifest to leave the service always active? I also tried with stiky, I tried to reactivate the service even in the sub destroy, but nothing to do, when I close the app the service stops working and never start again.
 

KMatle

Expert
Licensed User
Longtime User
I know that from my P8. Only known apps will run if the screen turns off. Add your app to the whitelist (options -> energy savings). When it first came out, even WhatsApp and Google Calendar wasn't on the whitelist and I wondered why I did not get any notifications. Now I have a Mate 10. All apps are now allowed to run in thenbackground.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
We had that discussion once. It doesn't matter where your app is installed from. It's upon the user. You can't do anything except tell the user to check the energy saving options because there is no api to check that on within your app. Should no be a big thing as it is only on some devices. Check it (once your app is online) via console how many devices are affected (by model). I assume it's only a few. Maybe Huawei did an update fir the P8 series, too.
 
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
Hi Erel, can you give me the video link?

PS.
my app does not receive messages, it has a socket that must always remain connected, other devices transmit data when necessary.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
my app does not receive messages, it has a socket that must always remain connected, other devices transmit data when necessary.
You must have a foregound service and acquire a partial lock with PhoneWakeState.

You should still expect the process to be killed from time to time.
 
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
Is possible to resolve problem with an intent that turn on service when it going down? But in this case the service destroy event don't start, wich solution to intercept the service stopped? If the whatsapp work with huawei P8 the solution exist, please help me :)

PS.
I' ve find information about PhoneWakeState, but this solution have effect on battery duration…


UPDATE:
I tryed with this in service_start:

B4X:
 Dim p As PhoneWakeState
 p.PartialLock

don't work, the service allways killed….
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Whatsapp has nothing to do with it. It uses push notifications. It doesn't use any foreground service.

I' ve find information about PhoneWakeState, but this solution have effect on battery duration…
The problem is not in PhoneWakeState. The problem is that you want the OS to keep the process running all the time.

I tryed with this in service_start:
It will not work without a foreground service.

The correct solution in most cases is to use push notifications to wake up your app.
 
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
Sorry Erel, but WhatsApp use push notifications?

I do not want to depend on google, I need the socket to remain connected to the server always, without interruption, and to do this I set up a connection system in case of errors or loss of connection, but if the phone kills me the service is over ! So I just need to know how to keep the service active even if a phone destroys it, and without going to google. If this can be done by whatsapp, why should not I be able to do it?
 
Last edited:
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
ok let's analyze the problem from another point of view: are you telling me that it is impossible to keep a service always active on devices like huawai P8 without resorting to google? Are you sure there is not a different solution?
It 's quite absurd that I have to resort to google to use an app of my creation, do not?


I have look this your suggest: https://www.b4x.com/android/forum/threads/custom-websocket-based-push-framework.40272/

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

Sub Service_Start (StartingIntent As Intent)
 StartServiceAt(Me, DateTime.Now + 2 * DateTime.TicksPerMinute, False)
 If wsh.IsInitialized = False Or wsh.ws.Connected = False Then
  Connect
 End If
 If IsPaused(Main) = False Then
  CallSub2(Main, "UpdateId", id)
  CallSub2(Main, "UpdateStatus", wsh.ws.Connected)
  'Show stored messages
  Dim messages As List = kvs.GetObject("messages")
  If messages.Size > 0 Then
   CallSub2(Main, "NewMessage", messages)
   messages.Clear
   kvs.PutObject("messages", messages)
  End If
 End If
End Sub

But if the huawei P8 kill service this push solution do not work, no?
 
Last edited:
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
I have put:

in Service_Create:
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS

in Service_start:
B4X:
p.PartialLock

in Service attribute:
B4X:
#Region  Service Attributes
 #StartAtBoot: true
 #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

in the manifest:
B4X:
AddPermission(android.permission.WAKE_LOCK)

but the huawei Always kill service…...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can only repeat what I already wrote:

You must have a foregound service and acquire a partial lock with PhoneWakeState.

You should still expect the process to be killed from time to time.

A sticky service will not help.

The issue has nothing to do with your specific phone.
I have put:

in Service_Create:
B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
This is not relevant. You need to call Service.StartForeground.

The most reliable way is to use push notifications. Push notifications is a system service.
 
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
Service.StartForeground for start my service? but the Service.StartForeground is not used for start notify?

this is the phone: Manufacturer HUAWEI; Model ALE-L21; Product ALE-L21; SdkVersion 23
 
Upvote 0

nicolino33

Active Member
Licensed User
Longtime User
maybe I understand... if I activate a fixed notification with Service.StartForeground the service is no longer killed? but if so, the notification icon would always be present, and it's not nice.
 
Upvote 0
Top