Android Question Service_TaskRemoved

Semen Matusovskiy

Well-Known Member
Licensed User
I need non-killable service.

At first, I tried to restart the Starter service using StartServiceAtExact ("", DateTime.Now + 1000, True) .
Works, but appears a red line in the log file - The Starter service should never be started from a receiver.
As I understand, it's neccessary to start another service (not very comfortable).
That's why I tried inline Java code (found on stackoverflow)
B4X:
    PendingIntent service = PendingIntent.getService (getApplicationContext (), 0, new Intent (getApplicationContext (), starter.class), PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmManager = (AlarmManager) getSystemService (Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, service);
Also works.
So, first question - is it safe ? I read some discussions and didn't see that it's not allowed to restart itself.

Another question.
I tried also to use Service_TaskRemoved in additional service module.
B4A doesn't include Sub Service_TaskRemoved in bas file. But generated java-code includes
B4X:
    public void onTaskRemoved(android.content.Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        if (false)  processBA.raiseEvent(null, "service_taskremoved");
          
    }

Why (false) ? B4A doesn't want TaskRemoved in services other than Starter ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I really don't understand what you are doing...

Just add another service and start it with StartServiceAt.

Note that newer versions of Android will not let you start the process too frequently.

TaskRemoved is a global event. In B4A is it handled in the Starter service. There is no reason to handle it in all services.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I have existing project with services. In one service, which has own name, I wanted to add
B4X:
Sub Service_TaskRemoved
       StartServiceAtExact ("", DateTime.Now + 1000, True) 
End Sub
and found that I can't do it.

Really, it's not a problem, I can add Starter service to my project. For me is interesting a nature of limitations.
If I understand correctly, this is simply "design preferences", nothing more.
 
Upvote 0
Top