I'm developing some applications with kotlin that will communicate with each other. This case affects 2 of those applications: a service and an application to play videos. The development is for a specific device with Android TV (Android 11).
The service has a web socket that is listening for commands. One of those commands is "play". With this command the service must run the application to play videos.
I've been using this code before:
val intent = Intent()
intent.setClassName("my.videoapp", "my.videoapp.MainActivity")
intent.putExtra("command", "play")
intent.putExtra("url", "https://www.example.com/samplevideo.mp4")
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
It works in the following cases
It doesn't work when the command comes from the socket. I guess it may have to do with the fact that it is running in another thread. Do you know how I could make it work?
I tried running the code with
Handler(Looper.getMainLooper()).post {
val intent = Intent()
...
context.startActivity(intent)
}
But doesn't work.
Starting an activity from background or foreground services is no longer allowed. It has been like this since Android 10.
https://developer.android.com/guide/components/activities/background-starts