Unity3D Android 插件:无法启动服务 [英] Unity3D Android plugin: unable to start service

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

问题描述

我的目标是启动一个服务,它是通过 .jar 文件作为 Unity3D 中的 android 插件添加的.在 这个线程中,我发现了如何启动它,我最终可以使用本机代码.但是我在日志中遇到了以下问题:

My aim is to start a service, that is added via .jar file as an android plugin in Unity3D. In this thread I found out how to launch it, I can finnaly get to native code. But I've encountered the following problem in the log:

07-14 15:02:23.965: W/ActivityManager(444): Unable to start service Intent { cmp=net.calipssoone.bnh/com.activitychecker.adservice.CheckService } U=0: not found

我用谷歌搜索并发现问题出在清单中,但无法弄清楚我做错了什么.以下是在清单中声明服务的方式:

I googled and found out that the problem is in the manifest, but couldn't figure out what am I doing wrong. Here's how the service is declared in the manifest:

  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true">
<service android:name="com.activitychecker.adservice.CheckService"/>
<receiver android:name="com.activitychecker.adservice.StartReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    <action android:name="CheckService" />
  </intent-filter>
</receiver>

它在Java中的包名其实是一样的:com.activitychecker.adservice

Its package name in Java is actually the same: com.activitychecker.adservice

StartReceiver 类:

StartReceiver class:

    public class StartReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {}
}

CheckService 类:

CheckService class:

public class CheckService extends Service {
    public void onCreate(){}
    public long getCurrentTime(){}
    public void loadInfo(){}
    public int onStartCommand(Intent intent, int flags, int startId){}
    public void onDestroy() {}
    public IBinder onBind(Intent intent) {}
    public class MyThread extends Thread { 
        public void run() {}
        public void cancel() {}
        public boolean check(String bundle){}
    }
    private class ScreenBroadcastReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {}
    }
}

UPD:我已经改变了我的清单:

UPD: I've changed my manifset from:

<service android:name="com.activitychecker.adservice.CheckService"/>

致:

<service android:name="com.activitychecker.adservice.CheckService"></service>

并且日志错误改为:

07-14 17:46:13.455: W/ActivityManager(444): Unable to start service Intent { act=com.activitychecker.adservice.CheckService } U=0: not found

推荐答案

当我尝试使用 Intent 启动服务时遇到了同样的异常.当我使用 Context 时它起作用了.因此,将上一个问题中的代码替换为下面使用 Context 而不是 Intent 的代码:

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 Android 插件:无法启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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