我想使Flutter应用程序始终在后台运行。使用android,我们必须创建一个始终在后台运行的服务。在Flutter文档中找不到关于服务的任何信息。
Flutter可以做这种事情吗?
我构建了一个待办事项列表应用程序,该应用程序应该显示通知以提醒任务。为了能够将通知安排在截止日期的准确时间,我将通知数据从 flutter 传递到 kotlin,并显示来自广播接收器的通知。
这里我将通知数据发送到 kotlin:
await platform.invokeMethod('setNextNotification', {
'tasksNames': tasksNames,
'notificationsTimeInMillis': notificationsTimeInMillis
});
Run Code Online (Sandbox Code Playgroud)
这就是我在 FlutterActivity 中获取数据的方式:
private const val CHANNEL = "flutter.native/helper"
class MainActivity : FlutterActivity() {
companion object {
const val TASKS_NAMES_EXTRA = "tasksNames"
const val NOTIFICATIONS_TIME_IN_MILLIS_EXTRA = "notificationsTimeInMillis"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
// Init the AlarmManager.
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
// We got here from the setNotifications() method in flutter...
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "setNextNotification") { …Run Code Online (Sandbox Code Playgroud) 我想在用户断开手机通话时显示自定义弹出消息。问题是如何检测应用程序何时未运行。任何线索都会有所帮助。