簡體   English   中英

如何從 Flutter 發送 ACTION_APPWIDGET_UPDATE 廣播

[英]How can I send a ACTION_APPWIDGET_UPDATE Broadcast from Flutter

在我的“java”應用程序中,我的 getDataFromServer() function 中有這段代碼(我的 WorkManager 每 2 小時調用一次,我的 MainActivity Onrefresh() 調用),以便用新數據更新我的主屏幕小部件:

AppWidgetManager.getInstance(context).getAppWidgetIds(
        new ComponentName(context,MyWidgetProvider.class));
Intent updateIntent = new Intent();
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(MyWidgetProvider.WIDGET_ID_KEY, ids);
context.sendBroadcast(updateIntent);

這將使 my,MyAppWidgetProvider “喚醒”,從文件中讀取新數據並進行更新。

現在,我已將我的邏輯移至 Flutter,我不知道如何從我的 Dart 代碼中調用此代碼。

有什么建議嗎?

可以參考 package https://pub.dev/packages/home_widget

在本機部分https://github.com/ABausG/home_widget/blob/3de5ff703e9f8f7700036a23e609e0627976f904/android/src/main/kotlin/es/antonborri/home_widget/HomeWidgetPlugin.kt#L

"updateWidget" -> {
                val className = call.argument<String>("android") ?: call.argument<String>("name")
                try {
                    val javaClass = Class.forName("${context.getPackageName()}.${className}")
                    val intent = Intent(context, javaClass)
                    intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
                    val ids: IntArray = AppWidgetManager.getInstance(context.applicationContext).getAppWidgetIds(ComponentName(context, javaClass))
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
                    context.sendBroadcast(intent)
                } catch (classException: ClassNotFoundException) {
                    result.error("-3", "No Widget found with Name $className. Argument 'name' must be the same as your AppWidgetProvider you wish to update", classException)
                }
            }

在 Dart 部分用於Background Update
https://pub.dev/packages/home_widget#background-update
由於 HomeWidget 的方法是HomeWidget ,因此即使 App 在后台,也可以在后台使用HomeWidget來更新 Widget。
示例 App 使用flutter_workmanager插件來實現這一點

https://github.com/ABausG/home_widget/blob/3de5ff703e9f8f7700036a23e609e0627976f904/example/lib/main.dart#L10

void callbackDispatcher() {
  Workmanager.executeTask((taskName, inputData) {
    debugPrint('Native called background task: $taskName');

    final now = DateTime.now();
    return Future.wait<bool>([
      HomeWidget.saveWidgetData('title', 'Updated from Background'),
      HomeWidget.saveWidgetData('message',
          '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}:${now.second.toString().padLeft(2, '0')}'),
      HomeWidget.updateWidget(
          name: 'HomeWidgetExampleProvider', iOSName: 'HomeWidgetExample'),
    ]).then((value) {
      return !value.contains(false);
    });
  });
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Workmanager.initialize(callbackDispatcher, isInDebugMode: kDebugMode);
  runApp(MyApp());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM