本文介绍了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");
}

评论是否有问题.

这篇关于Unity3D Android插件:无法启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:37