Reputation: 177
I have a Unity app that runs on Android. It also continues to run when the app is placed into the background. It does not use a service to run in the background. Instead, this app creates a new thread, and this thread continues to run when the app is in the background.
Recently, I have had to merge in some code from another app into this app. This other app has an activity that I merged into the first app.
The activities from the different app have different names. In my Unity app that runs in the background, its activity is "com.unity3d.player.UnityPlayerNativeActivity" The activity that I am merging in is "com.indotraq.android.rtls.MainActivity" which extends UnityPlayerActivity
Through experimentation, it appears that if my activity is not a "com.unity3d.player.UnityPlayerNativeActivity", my thread that runs in the background no longer executes.
So, does anybody have any ideas why my thread in the background will no longer execute and what I could do to get it executing again?
The other option that I am aware of it to write a service that will allow my program to process information when it is in the background. The downside of this approach is that I would have to rewrite a bunch of code from C# to Java. And some of this code is not just a simple port. I guess I would have to rewrite some Unity low-level networking code to use sockets.
So any advice on how to proceed? It would be great to get my thread running the background again so I don't have to rewrite a bunch of C# code to Java.
Thanks in advance John Lawrie
Upvotes: 1
Views: 15757
Reputation: 1736
RuninBackgorund is ignored for Android and IOS how you can see here
The only and efficient way to run in background on Android is through a Service. You can use OnApplicationPause method to get notified when the game pauses/unpauses.
public void OnApplicationPause(bool paused) {
if(paused) {
// Game is paused, start service to get notifications
} else {
// Game is unpaused, stop service notifications.
}
Here you have a nice tutorial about communication between android and unity
Upvotes: 0