如何在颤振中使用工作管理器运行后台进程?



我想使用 Flutter Workmanager,我在我的 .kt 中做了引用的配置,就像那样

package com.example.mybackprocess
import be.tramckrijte.workmanager.WorkmanagerPlugin
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant
class App : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
WorkmanagerPlugin.setPluginRegistrantCallback(this)
}
override fun registerWith(reg: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(reg)
}
}

我已经将安卓:名称更改为

android:name=".App"

但它给了我这个错误:

Launching libmain.dart on G3212 in debug mode...
e:E:mybackprocessandroidappsrcmainkotlincomexamplemybackprocessMainActivity.kt: (15, 48): 
Type mismatch: inferred type is PluginRegistry? but FlutterEngine was expected
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more 
log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 55s
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

文档有问题。你可以试试这个

不要删除您的.MainActivity.kt,而是删除您的.App类。

MainActivity.kt

package your_package_name
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity 
import io.flutter.embedding.engine.FlutterEngine    
import io.flutter.plugins.GeneratedPluginRegistrant 
class MainActivity: FlutterActivity() { 
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {    
GeneratedPluginRegistrant.registerWith(flutterEngine);  
}   
}

安卓清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your_package_name">
<uses-permission android:name="android.permission.INTERNET" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application android:label="xxx" android:usesCleartextTraffic="true" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<!-- <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" /> -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
<!-- Theme to apply as soon as Flutter begins rendering frames -->
<meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
</activity>
<meta-data android:name="flutterEmbedding" android:value="2" />
</application>
</manifest>

样式.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
</resources>

最新更新