Java Android服务不是从Unity启动的
我正在用Unity开发一个android应用程序,它需要一个前台服务。为此,我用android studio制作了一个android模块。问题是,我的服务从未启动,但同时没有错误,应用程序仍在运行,例如,在我的启动服务之后,我可以举杯祝酒 Unity-C#-AndroidPluginManager.cs 我使用这个类来实例化模块并调用函数来启动服务。使用Unity inspector的onClick()特性调用函数StartAndroidService()Java Android服务不是从Unity启动的,java,c#,android,unity3d,android-service,Java,C#,Android,Unity3d,Android Service,我正在用Unity开发一个android应用程序,它需要一个前台服务。为此,我用android studio制作了一个android模块。问题是,我的服务从未启动,但同时没有错误,应用程序仍在运行,例如,在我的启动服务之后,我可以举杯祝酒 Unity-C#-AndroidPluginManager.cs 我使用这个类来实例化模块并调用函数来启动服务。使用Unity inspector的onClick()特性调用函数StartAndroidService() using System.Collec
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AndroidPluginManager : MonoBehaviour
{
private AndroidJavaObject androidPlugin = null;
private AndroidJavaObject unityContext = null;
private AndroidJavaObject unityActivity = null;
private AndroidJavaClass unityClass = null;
void Start() {
//Get unity activity and context
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");
//Create android plugin object
androidPlugin = new AndroidJavaObject("com.androidplugin.stepcounterlibrary.ApiStepCounter");
//Set activity and context to the module
androidPlugin.Call("setActivity", unityActivity);
androidPlugin.Call("setContext", unityContext);
}
//Start Service
public void StartAndroidService() {
androidPlugin.Call("startStepCounter");
androidPlugin.Call("showMessage", "Hello World");
}
}
Android Studio-Java-StepCounterService.Java
这个类应该启动前台服务并显示通知,但是它没有。这是我第一次尝试使用Android服务,所以我不知道自己是否做错了什么。此外,我还尝试在每个方法(onCreate()、onStartCommand()、onDestroy())中执行一些Log.d,但没有打印任何日志,因此我猜服务没有启动
package com.androidplugin.stepcounterlibrary;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
public class StepCounterService extends Service {
public StepCounterService(){}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "TestChannel");
builder.setSmallIcon(R.drawable.ic_android)
.setContentText("Text of notification")
.setContentTitle("Title of notification");
Notification notification = builder.build();
startForeground(1, notification);
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Android Studio-Manifest-AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidplugin.stepcounterlibrary">
<application>
<service android:name=".StepCounterService"/>
</application>
</manifest>
我已经试了两天如何使用Unity和Android模块创建前台服务,如果有人能帮助我,我会非常感激。请随时问我您需要的任何问题,我想这就是创建前台服务所需的全部内容,但可能我遗漏了一些内容。startStepCounter()中您收到了什么呼叫?从Unity到Android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidplugin.stepcounterlibrary">
<application>
<service android:name=".StepCounterService"/>
</application>
</manifest>