Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start android service from Unity3D code

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.

like image 917
Sam Stone Avatar asked Jul 14 '16 08:07

Sam Stone


People also ask

How do I start an Android service?

Starting a service You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

Can I develop Android app in Unity?

The most effective one is to build and run the app on an Android phone as you go along. Unity actually allows you to manage this easily, provided you have the correct drivers installed. Before you can actually build and test the app, you will need to set up a couple of things. Go to Edit > Project Settings > Player.

How do I enable Android player in Unity?

Go to the Unity Editor. From the menu, choose 'File', 'Build Settings…' In the Build Settings dialog, under 'Platform', select 'Android' as the target and click 'Switch Platform'.

How does Unity run on Android?

When Unity builds an Android app, it includes a . NET bytecode interpreter in native code, based on Mono. When you run the app, the interpreter is run to execute the bytecodes. That's how it can run on Android.


1 Answers

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.

like image 62
Programmer Avatar answered Sep 25 '22 20:09

Programmer