Hosein_Mghr

Member
In Android 13, I did everything to be able to restart the service after destroying it. Android 13 does not allow restarting. I tried StartService and StartServiceAt, but it didn't work. Now I want to know if it can be done with the new b4a receivers from the intent action. I should make a TIME_TICK receiver so that it will be called once every minute and it will work. Or if anyone has a solution related to StartService, please help me. If I spelled wrong, I'm sorry, my language is not English. Thank you.
 

Hosein_Mghr

Member
this Is Monifist:
AddReceiverText(Receiver,<intent-filter><action android:name="android.intent.action.TIME_TICK" /></intent-filter>)

this Is Receiver:
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
              
             If StartingIntent.Action = "android.intent.action.TIME_TICK" Then
                 Log(" TIME TICK")
                ToastMessageShow("Time Tick", True)
              End If
            
End Sub



These are my codes, everything seems to be correct, but it doesn't work
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
Receivers aren't new they're just how things should be done.

Once every minute doesn't work well, it needs to be longer than that.

Start your app. Set up a receiver to fire in 10 minutes. Capture the the event , make sure your app is doing what you want (if not, start it, if it is then leave it alone). Set another receiver to fire in 10 minutes....
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I don't understand well, can you explain better?
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
I don't understand well, can you explain better?
Make a startup receiver. This gets fired when your device boots up.

Add the following to your manifest file (note the name StartupReceiver must match your receiver name in your code)

B4X:
AddPermission(android.permission.RECEIVE_BOOT_COMPLETED)
AddReceiverText(StartupReceiver, <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>)

Next create a receiver called StartupReceiver in your code.

B4X:
Sub Process_Globals
 
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
 
    'This receiver is called when the device has successfully rebooted.
    'All we're going to do here is start the NotifyReceiver
    'which will set itself to run every ten minutes

    StartReceiverAt(NotifyReceiver, DateTime.Now + 10 * DateTime.TicksPerMinute, True)    
End Sub

Then we create another receiver - NotifyReceiver in my case -


B4X:
Sub Process_Globals
 
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)

    'Use a keyvalue store to pass info
 
    Log ("NotifyReceiver Fired!")

    'Do whatever you neeed here - Start an activity, fire off a reminder, whatever.

    StartReceiverAt (Me, DateTime.Now + 10 * DateTime.TicksPerMinute  , True)            'Set a new receiver to fire in 10 minutes time
 
End Sub

Remember - Receivers get killed off quite quickly so if you need to do a lot of processing then you need to start an activity. Also don't expect your receiver to fire exactly ten minutes after it's been set. I've found it's rarely on time.
 
Last edited:
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
The above only starts the receiver at boot up time. You can of course simply run
B4X:
StartReceiverAt(NotifyReceiver, DateTime.Now + 10 * DateTime.TicksPerMinute, True)
in Activity_Create so it gets set when you open your app.
 
Upvote 0

Hosein_Mghr

Member
Thank you for your help, dear friend.
My main problem is StartServiceAt or StartService. I tried it on xiaomi sdk 33 devices. In services, after destroying the service, it cannot be restarted and the program crashes, so I am looking for a solution. I have a Tcp socket in Android 10. It works well, it reconnects every two minutes, but in Android 13, when the service is closed, it crashes from StartService or StartServiceAt which I use. I did not find any solution. Now I will test your method.
Thanks
 
Upvote 0

Hosein_Mghr

Member
I don't understand well, can you explain better?
If you have ever noticed the service that has been closed, for example with the action: android.provider.Telephony.SMS_RECEIVE
to be called, it will be launched again by Android 13. Now I want to use an action that is repetitive, such as: Action TIME_TICK

And call my service or receiver every few minutes so that I can have a stable server in Android 13
 
Upvote 0

Hosein_Mghr

Member
Thank you very much, correct
It works well, thank you
If you have ever noticed the service that has been closed, for example with the action: android.provider.Telephony.SMS_RECEIVE
to be called, it will be launched again by Android 13. Now I want to use an action that is repetitive, such as: Action TIME_TICK

And call my service or receiver every few minutes so that I can have a stable server in Android 13
Make a startup receiver. This gets fired when your device boots up.

Add the following to your manifest file (note the name StartupReceiver must match your receiver name in your code)

B4X:
AddPermission(android.permission.RECEIVE_BOOT_COMPLETED)
AddReceiverText(StartupReceiver, <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>)

Next create a receiver called StartupReceiver in your code.

B4X:
Sub Process_Globals
 
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
 
    'This receiver is called when the device has successfully rebooted.
    'All we're going to do here is start the NotifyReceiver
    'which will set itself to run every ten minutes

    StartReceiverAt(NotifyReceiver, DateTime.Now + 10 * DateTime.TicksPerMinute, True)   
End Sub

Then we create another receiver - NotifyReceiver in my case -


B4X:
Sub Process_Globals
 
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)

    'Use a keyvalue store to pass info
 
    Log ("NotifyReceiver Fired!")

    'Do whatever you neeed here - Start an activity, fire off a reminder, whatever.

    StartReceiverAt (Me, DateTime.Now + 10 * DateTime.TicksPerMinute  , True)            'Set a new receiver to fire in 10 minutes time
 
End Sub

Remember - Receivers get killed off quite quickly so if you need to do a lot of processing then you need to start an activity. Also don't expect your receiver to fire exactly ten minutes after it's been set. I've found it's rarely on time.
Thank you very much, correct
It works well, thank you
 
Upvote 0
Top