从Unity3D代码启动android服务 [英] Start android service from Unity3D code

查看:506
本文介绍了从Unity3D代码启动android服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android Unity3D应用程序中,我需要启动一个服务,该服务将在后台运行.我不知道该怎么办.方法 startService()必须在活动上调用,但我不知道如何将当前的unity活动从unity脚本传递到我的android插件.而且我还没有找到以静态方法获取活动并在其中运行 startService()的任何方法.

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.

据我了解的顺序,我需要获取主要的Unity3D活动并从中启动服务.

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));
    }
}

此代码不起作用,因为无法解析方法startService",并且我没有任何要传递的 this 参数.我需要获取当前活动.

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.

推荐答案

以下是将Activity实例/引用发送到使用onCreate函数或从UnityPlayerActivity扩展.

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

方法1 :发送Activity参考一次,然后将其存储在Java中的静态变量中以备重复使用:

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");
}

方法2 :在每个函数调用中发送Activity参考.

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);
}

注意:您必须用StatusCheckStarter类的完整软件包替换com.example.StatusCheckStarter.

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

这篇关于从Unity3D代码启动android服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆