Android Java:仅MainActivity可调用startForegroundService,其他类调用失败求助
问题
在Android Java中,在MainActivity里调用startForegroundService可正常运行,但通过与Unity交互的Bridge类调用时却无法生效。
可正常运行的MainActivity代码:
package com.example.test; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent serviceIntent = new Intent(this, ForegroundService.class); startForegroundService(serviceIntent); } }
无法运行的Bridge类代码:
package com.exemple.test; import android.app.Activity; import android.app.Application; import android.content.Intent; public class Bridge { public static Activity unityActivity; public static void receiveUnityActivity(Activity tActivity){ unityActivity = tActivity; } public static void Start(){ unityActivity.startForegroundService(new Intent(unityActivity, ForegroundService.class)); } }
Unity调用的C#脚本:
AndroidJavaClass unityClass; AndroidJavaObject unityActivity; AndroidJavaClass customClass; void Start() { sendActivityReference("com.example.test.Bridge"); startService(); } void sendActivityReference(string packageName) { unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity"); customClass = new AndroidJavaClass(packageName); customClass.CallStatic("receiveUnityActivity", unityActivity); } void startService() { customClass.CallStatic("Start"); }
已确认AndroidManifest中服务配置和权限均正确,且C#脚本调用其他方法正常,请问问题出在哪里?
问题排查与解决
1. 包名拼写错误(核心问题)
Bridge类的包名写的是com.exemple.test(注意是exemple,少了字母l),但Unity C#代码中传递的包名是com.example.test.Bridge(正确拼写为example),这会导致Unity无法找到你编写的Bridge类,或者调用到错误的类,直接导致Start方法执行无效。
解决:把Bridge类的包名修改为package com.example.test;,保持和Unity中引用的包名一致。
2. 潜在的其他问题
- Activity实例有效性验证:在Bridge的
receiveUnityActivity方法中添加日志,确认unityActivity是否为null,确保Unity传递的Activity实例正确:public static void receiveUnityActivity(Activity tActivity){ unityActivity = tActivity; Log.d("Bridge", "Received Activity: " + (unityActivity != null ? "Valid" : "Null")); } - 启动时机延迟:Unity的
Start方法执行时,当前Activity可能还未完全初始化完成,尝试延迟调用startService:void Start() { sendActivityReference("com.example.test.Bridge"); Invoke("startService", 1f); // 延迟1秒执行 } - 动态权限检查:Android 9(API 28)及以上需要
FOREGROUND_SERVICE权限,即使Manifest中已声明,也可以在启动服务前检查权限:import android.Manifest; import android.content.pm.PackageManager; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public static void Start(){ if (unityActivity == null) { Log.e("Bridge", "Unity Activity is null"); return; } // 检查前台服务权限 if (ContextCompat.checkSelfPermission(unityActivity, Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(unityActivity, new String[]{Manifest.permission.FOREGROUND_SERVICE}, 1001); return; } unityActivity.startForegroundService(new Intent(unityActivity, ForegroundService.class)); } - ForegroundService必须调用startForeground:服务启动后5秒内必须调用
startForeground显示通知,否则会触发ANR,确保你的ForegroundService中实现了这一步:@Override public int onStartCommand(Intent intent, int flags, int startId) { // 创建通知并调用startForeground Notification notification = createForegroundNotification(); startForeground(1, notification); // 其他逻辑 return START_STICKY; }
内容的提问来源于stack exchange,提问作者Shawlong
相关产品推荐
相关产品推荐

