从 Unity3D 代码启动 android 服务 [英] Start android service from Unity3D code
问题描述
在我的 Unity3D 应用程序中,我需要启动一个服务,该服务将在后台运行.我不知道我该怎么做.必须在活动上调用方法 startService(),但我不知道如何将当前的统一活动从统一脚本传递到我的 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
实例/引用到不使用的Java插件的方法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屋!