How to to run a function in background even after the app is closed?

How to create an extension to run a function in background even after the app is closed?

If anyone knows the way, please help me.

The similar extension is already present :point_down:

As soon as MIT releases new version it will be possible to use Services in extensions so you can create an extension using official method.

3 Likes

Where did you get that information?

1 Like

@vknow360 Ok, I got a message from Evan Patton (MIT team) in the AI2 power user forum:

Assuming that Kodular is keeping up with MIT’s master, a somewhat recent pull request added the ability for extensions to declare service elements in the AndroidManifest. I actually built a similar extension to test that PR by just having a background clock going in an extension to drive UI updates even when the screen was off. This functionality is part of the upcoming nb184 release (You can check out master and use the extensions in ai2-test).

@Hossein might be able to speak more to where Kodular stands w.r.t. support for services in extensions.

Edit: Here is the PR in question:

github.com/mit-cml/appinventor-sources

3 Likes

And it is just of because of Shreyash that we shall be able to use Services in our extensions.A lot of thanks to him :heart_eyes:
Now we can say that App Inventor = Android Studio :grin:

7 Likes

Can i have link to his posts?

@bodymindpower has already posted PR link.

Yes, almost. But, as far as I have understood so far, you still need an extension to be able to execute BackgroundTasks. Is that correct?

1 Like

@Shreyash can you please shed some light on these (mysterious) background tasks? :wink:

Since the beginning of AppInventor and its clones, it has been asked, discussed, complained etc. And now it should be possible

1 Like

Yes, you are right. Extension developers can write services, but not app developers themselves. So you might see purpose-built background task extensions in the future that cater to a few users’ needs. Or perhaps someone could write an extension that takes the name of a procedure and runs it in the background? :thinking:

5 Likes

Definitely true. This is the time to break the barriers. Limitations pause the development.

Services are one of four fundamental app components of Android (other three being Activities, Broadcast Receivers and Content Providers). They can perform long running tasks in background, even after the application is closed, for an indefinite period of time.
Some simple use cases of services:

  • Playing music in background
  • Fetching data from a remote server.
  • Downloading files from the internet.

Like Vishwas mentioned, this implementation of services doesn’t directly let app developers to use services currently. They still need to depend on extension devs to write extensions for their specific use case. For example, there could be an extension for playing music in background or a file downloader extension that works even when the app is closed.

And talking about the extension for running procedures in background, I don’t really think it is a good idea. I’d rather there should be inbuilt blocks that would let users harness all the powers of services. Maybe in near future, this might be possible.

8 Likes

Thanks alot for the detailed information!

However, I am not sure what that means exactly or what could cause problems.

Yes, that would be great.

3 Likes

Thank you Shreyas for your detailed reply.
If this barrier is cut, we can develop even more innovatieve extensions which could help the app developers to stretch their wings broader.

It lets the kiwi fly !

3 Likes

It is still possible,
With this repo by @jarlissonlira2 : GitHub - jarlisson2/appinventor-sources at edi1
I am able to generate my BackgroundTasks extension with service element in it,
Now you would have a question in mind that how can it work without update
It’s because main extension information is stored in 4 Json Files inside extension in which 2 of them contains all the annotation and if you add service there then it will work(As a activity inside the json)(Not in extension)

4 Likes

This should finally solve the problem with the outdated ExoPlayer version:
[ grafik ] → @Mika

See e.g. here:

1 Like

Kodular tried to implement Tasks in the past. But they stopped implementing because of bugs
Why not Kodular try again implementing it?
App Inventor implemented but they shutdown the service
f93acbd7dca6bdec4145ba051068147942b4a0fd
What are Tasks?
You can drag and drop invisible components like clock on tasks screen and you can program them in blocks which works in background

Yes, we already know this:

2 Likes

I still don’t understand how it will work. For the compiler to be able to understand these annotations, some changes are required to be done in the Compiler.java and other files, so that the compiler would be able to process them, and, for this particular case, add the <service> tag in AndroidManifest.xml.

1 Like