简体   繁体   English

如何控制在我的应用程序在颤振/飞镖的后台活动期间创建的 object?

[英]How to control an object that's been created during my app's background activity in flutter/dart?

I've been making this flutter app where it plays an alarm while receiving a notification.我一直在制作这个 flutter 应用程序,它会在收到通知时播放警报。 When my app is in the foreground, I am able to play or stop the alarm.当我的应用程序在前台时,我可以播放或停止闹钟。 When it is in the background, once the alarm starts playing after receiving a notification, I am unable to stop it.当它在后台时,一旦收到通知后警报开始播放,我就无法停止它。 (It seems like a different instance is being created while in the background. But unfortunately, the FlutterRingtonePlayer.playAlarm() does not return any references to control it.) Is there any better/alternative way of making this work? (似乎在后台创建了一个不同的实例。但不幸的是,FlutterRingtonePlayer.playAlarm() 没有返回任何引用来控制它。)有没有更好/替代的方法来完成这项工作?

Here is my code这是我的代码

import 'package:flutter_ringtone_player/flutter_ringtone_player.dart';

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print("Handling a background message");
  FlutterRingtonePlayer.playAlarm();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
    _firebaseMessaging.subscribeToTopic('class');
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
    }
       @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
              ElevatedButton(
                  onPressed: () => FlutterRingtonePlayer.playAlarm(),
                  child: Text('Play Alarm')),
              ElevatedButton(
                  onPressed: () => FlutterRingtonePlayer.stop(),
                  child: Text('Stop Alarm')),
            ])));
  }
}

I've solved this problem by using Android Alarm Manager https://pub.dev/packages/android_alarm_manager and running both the Play and Stop methods in the background whether the app is in foreground or background.我已经通过使用 Android 警报管理器https://pub.dev/packages/android_alarm_manager并在后台运行 Play 和 Stop 方法解决了这个问题,无论应用程序是在前台还是后台。

Instead of FlutterRingtonePlayer.PlayAlarm() and FlutterRingtonePlayer.Stop(), use AndroidAlarmManager.oneShot(Duration.zero, 0, _playAlarm);代替 FlutterRingtonePlayer.PlayAlarm() 和 FlutterRingtonePlayer.Stop(),使用 AndroidAlarmManager.oneShot(Duration.zero, 0, _playAlarm); and AndroidAlarmManager.oneShot(Duration.zero, 0, _stopAlarm);和 AndroidAlarmManager.oneShot(Duration.zero, 0, _stopAlarm);

{//to play or pause the alarm
onPress()=>AndroidAlarmManager.oneShot(Duration.zero, 0, _playAlarm);
onPress()=>AndroidAlarmManager.oneShot(Duration.zero, 0, _stopAlarm);
}
// top level functions
void _playAlarm() {
  FlutterRingtonePlayer.playAlarm(volume: 1, looping: true, asAlarm: true);
}
void _stopAlarm() {
  FlutterRingtonePlayer.stop();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当颤振应用程序自动启动处于 BOOT_COMPLETED 时,在后台执行 Dart 函数? - Execute a Dart function in background when the flutter app Autostart's on BOOT_COMPLETED? 如何控制我的应用程序的版本? - How to control my app's version? 如何让我的音量控制应用程序在颤动的系统音量变化中立即响应(飞镖) - How can I make my volume control app response instantly in system volume change in flutter(dart) 如何在AsyncTask中更新Activity的变量? - How to update Activity's variable during AsyncTask? 如何在片段中创建的活动中添加回调? - How to add a callback to an activity that's created in a fragment? 为什么我用 Flutter 创建的 iOS 应用程序的大小为 475MB? - Why my iOS app's size created with Flutter is 475MB? 如何通过服务控制活动的内容? - How to Control an Activity's Contents from a Service? 应用程序进入后台后如何导航回特定活动? - How to navigate back to specific Activity after app has been in background? 从我的主应用程序的活动中启动智能手表上的控件扩展。 还会在手表上显示从活动发送的文本 - Start a control extension on the smart watch from my main app's Activity. Also display a text on the watch which is sent from the activity 如何让我的应用程序始终在用户手机的后台运行 - How to allow my app to run always in background in user's phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM