【发布时间】:2021-06-01 04:03:34
【问题描述】:
我正在尝试在启动时启动前台服务。 为了实现这一点,我使用flutter_foreground_plugin 我在 android 的应用程序清单中添加了一个 BOOT_COMPLETED 接收器,广播接收器被正确调用,我可以在启动时在其中运行代码。 我的问题很简单——我似乎找不到 changjoopark.com.flutter_foreground_plugin.FlutterForegroundService 插件的正确名称来通过意图启动它。 注意前台插件的包名当然和我的不一样。
以下是 AndroidManifest 的相关摘录:
<receiver android:enabled="true" android:name=".ServiceStartup" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name="changjoopark.com.flutter_foreground_plugin.FlutterForegroundService"/>
这是服务启动的代码:
package com.somecompany.myapp;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
public class ServiceStartup extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
System.out.println(">>>>>> In onReceive of ServiceStartup <<<<<<< ");
Handler h = new Handler();
h.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent("changjoopark.com.flutter_foreground_plugin.FlutterForegroundService");
intent.setPackage("changjoopark.com.flutter_foreground_plugin");
context.startForegroundService(intent);
}
});
}
}
我尝试了所有可能的指定包名称和组件的组合,但似乎都不起作用,因为我在 logcat 中遇到了无法找到组件的错误。 我错过了什么?我应该如何启动服务?
谢谢!
【问题讨论】:
标签: android flutter package naming foreground-service