11

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Sam Stone
  • 477
  • 2
  • 10
  • 33

1 Answers1

11

Below are two ways to send Activity instance/reference to Java plugin that doesn't use the onCreate function or extend from UnityPlayerActivity.

Method 1: Send Activity reference once then store it in a static variable in Java for re-usual:

Java:

public final class StatusCheckStarter {

    static Activity myActivity;

    // Called From C# to get the Activity Instance
    public static void receiveActivityInstance(Activity tempActivity) {
        myActivity = tempActivity;
    }

    public static void StartCheckerService() {
        myActivity.startService(new Intent(myActivity, CheckService.class));
    }
}

C#:

AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaClass customClass;

void Start()
{
    //Replace with your full package name
    sendActivityReference("com.example.StatusCheckStarter");

   //Now, start service
   startService();
}

void sendActivityReference(string packageName)
{
    unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    customClass = new AndroidJavaClass(packageName);
    customClass.CallStatic("receiveActivityInstance", unityActivity);
}

void startService()
{
    customClass.CallStatic("StartCheckerService");
}

Method 2: Send Activity reference in each function call.

Java:

public final class StatusCheckStarter {

    public static void StartCheckerService(Activity tempActivity) {
        tempActivity.startService(new Intent(tempActivity, CheckService.class));
    }
}

C#:

void Start()
{
    //Replace with your full package name
    startService("com.example.StatusCheckStarter");
}

void startService(string packageName)
{
    AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass customClass = new AndroidJavaClass(packageName);
    customClass.CallStatic("StartCheckerService", unityActivity);
}

Note: You must replace com.example.StatusCheckStarter with the full package of your StatusCheckStarter class.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    First method worked, I didn't even have to try the second one. Thank you. – Sam Stone Jul 14 '16 at 10:29
  • 1
    @TengkuFathullah With Android Studio. Compile it into *jar* or *aar* plugin extension and place it in your Unity *Assets/Plugins/Android* path. This is not the scope of this answer and you can literally google how to make Java plugin for Unity. – Programmer Feb 22 '18 at 02:40
  • @Programmer I have go through many website but stumble with android studio I have no idea whether I need to extend service class or activity. I have added new android module. – Tengku Fathullah Feb 22 '18 at 04:11
  • I really like the simple second method. It does not rely on a static member – CGMan Mar 03 '20 at 17:42