Start Android Service from Unity3D Code

Start android service from Unity3D code

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.

Unity3D Android plugin: unable to start service

I got the-same exception when I tried to start service with Intent. It worked when I used Context. So replace the code from your last question with the one below that uses Context instead of Intent:

Java:

public final class StatusCheckStarter {
static Context myContext;
// Called From C# to get the Context Instance
public static void receiveContextInstance(Context tempContext) {
myContext = tempContext;
}
public static void StartCheckerService()
{
myContext.startService(new Intent(myContext, CheckService.class));
}
}

C#:

AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaObject unityContext;
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");
unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

customClass = new AndroidJavaClass(packageName);
customClass.CallStatic("receiveContextInstance", unityContext);
}

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

Comment if there is any problem.

Unity3d Script from Native Android Service

I do not know about how to call unityscript from native code. But you can make a GameObject stay alive until manual removal by using the DontDestroyOnLoad function on it. Example:

void Awake() 
{
DontDestroyOnLoad(transform.gameObject);
}

More info from here.



Related Topics



Leave a reply



Submit