In my Unity3D application for android I need to start a service, which will run in background. I can't figure it out how can I do it. The method startService() has to be invoked on an activity, but I do not know how to pass the current unity activity from unity script to my android plugin. And I haven't found any way to get the activity in a static method and run startService() in it.
As far as I understand the sequence, I need to get main Unity3D activity and start the service from it.
My class which is supposed to call the service.
public final class StatusCheckStarter {
public static void StartCheckerService()
{
startService(new Intent(this, CheckService.class));
}
}
This code does not work, because "Cannot resolve method startService" and I have nothing to pass in this argument. I need to get the current activity.
Below are two ways to send
Activity
instance/reference to Java plugin that doesn't use theonCreate
function or extend fromUnityPlayerActivity
.Method 1: Send
Activity
reference once then store it in a static variable in Java for re-usual:Java:
C#:
Method 2: Send
Activity
reference in each function call.Java:
C#:
Note: You must replace
com.example.StatusCheckStarter
with the full package of yourStatusCheckStarter
class.