我一直在寻找这个,但没有找到任何包或在Flutter中安排后台任务的方法.就像在Android中一样WorkManager,AlarmManager.
我知道我可以使用这些类来访问这些类MethodChannel,但我想要一些适用于iOS和Android的东西.
(非常令人失望的是,移动框架无法安排后台任务).
我一直在尝试为 Flutter 编写平台代码以启动后台服务。在这里,我使用了一个没有完成实际工作的最小示例来表明应用程序根本无法运行。实际的颤振代码根本没有被修改。
主活动.java
public class MainActivity extends FlutterActivity {
Intent i = new Intent(this, MainService.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
Run Code Online (Sandbox Code Playgroud)
主服务.java
public class MainService extends IntentService {
public MainService() {
super("MainService");
}
@Override
protected void onHandleIntent(Intent Intent) {
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<service android:enabled="true"
android:name=".MainService"></service>
Run Code Online (Sandbox Code Playgroud)
buildVersion > 27 并且 Manifest 文件相应地service添加了标签。
编译并运行 withflutter run -v将显示以下消息:
..
[ +121 ms] Starting: Intent { act=android.intent.action.RUN flg=0x20000000 cmp=com.example.hello/.MainActivity (has extras)}
[ +1 …Run Code Online (Sandbox Code Playgroud) 我想在用户断开手机通话时显示自定义弹出消息。问题是如何检测应用程序何时未运行。任何线索都会有所帮助。
我正在 Flutter 应用程序中开发本机 Android 小部件。其中有刷新按钮,点击后我必须在 Flutter 代码中调用一个方法。我正在使用 Flutter Method Channel 进行通信,并且当应用程序处于前台时它工作正常。但是当应用程序最小化或关闭时它不起作用。我收到错误PlatformException(NO_ACTIVITY, null, null)。下面是我的代码。
安卓(AppWidgetProvider)
if (methodChannel == null && context != null) {
FlutterMain.startInitialization(context)
FlutterMain.ensureInitializationComplete(context, arrayOf())
// Instantiate a FlutterEngine.
val engine = FlutterEngine(context.applicationContext)
// Define a DartEntrypoint
val entrypoint: DartEntrypoint = DartEntrypoint.createDefault()
// Execute the DartEntrypoint within the FlutterEngine.
engine.dartExecutor.executeDartEntrypoint(entrypoint)
// Register Plugins when in background. When there
// is already an engine running, this will be ignored (although there will be some
// warnings in the …Run Code Online (Sandbox Code Playgroud)