Unity3D Android插件:无法启动服务

时间:2016-07-14 11:24:46

标签: c# android unity3d plugins service

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

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

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

  <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

StartReceiver类:

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

CheckService类:

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: 我改变了我的观点:

<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

2 个答案:

答案 0 :(得分:1)

当我尝试使用Intent启动服务时,我遇到了同样的异常。我使用Context时有效。因此,请将上一个问题中的代码替换为使用Context代替Intent的代码:

<强>爪哇

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

评论是否有任何问题。

答案 1 :(得分:0)

我发现解决方案直接将清单封装到assets / android文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
	android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>

    <application
		android:theme="@style/UnityThemeSelector"
		android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>
        <service android:name="com.addcomponent.unitynativeplugin.GeoLocation"/>
    </application>
</manifest>