【问题标题】:Unity3D Android plugin: unable to start serviceUnity3D Android插件:无法启动服务
【发布时间】:2016-07-14 11:24:46
【问题描述】:

我的目标是启动一项服务,该服务通过 .jar 文件作为 Android 插件添加到 Unity3D 中。在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

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

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

统一更新: 我已将清单更改为:

<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

【问题讨论】:

  • 如果您发布 CheckService 和 StartReceiver 类会很好。我不想看到它们里面的代码。只是它的蓝图和其中的功能。
  • 你试过this吗?
  • 我已经更新了问题代码,@Programmer
  • @UmairM 如果该答案的目的是通过字符串调用意图,我刚刚尝试过,但没有任何成功。我的清单中没有任何“远程”属性。服务属性在应用标签内,并且有完整的包路径,因为它在另一个包中。
  • 我今天做了实验,我想我找到了你的问题。不太确定,但这个问题解决了吗?

标签: c# android unity3d plugins service


【解决方案1】:

当我尝试使用Intent 启动服务时,我遇到了同样的异常。当我使用Context 时它起作用了。因此,将上一个问题中的代码替换为下面使用Context 而不是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");
}

有什么问题可以留言。

【讨论】:

    【解决方案2】:

    我找到了将清单直接复制到 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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多