Flutter - 在启动时启动前台服务

时间:2021-03-02 15:31:33

标签: android flutter package naming foreground-service

我正在尝试在启动时以颤振方式启动前台服务。 为了实现这一点,我正在使用 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 中收到了无法找到组件的错误。 我错过了什么?我应该如何启动服务?

谢谢!

0 个答案:

没有答案