在 Android O 中,如何在以编程方式固定小部件快捷方式的同时获取有关成功回调意图的任何操作



>我正在尝试使用 android O 中给出的 API 来固定小部件快捷方式,如

https://developer.android.com/guide/topics/appwidgets/index.html#Pinning

给出的示例代码是:

AppWidgetManager mAppWidgetManager = context.getSystemService(AppWidgetManager.class);
ComponentName myProvider = new ComponentName(context, MyAppWidgetProvider.class);
if (mAppWidgetManager.isRequestPinAppWidgetSupported()) {
    // Create the PendingIntent object only if your app needs to be notified
    // that the user allowed the widget to be pinned. Note that, if the pinning
    // operation fails, your app isn't notified.
    Intent pinnedWidgetCallbackIntent = new Intent( ... );
    // Configure the intent so that your app's broadcast receiver gets
    // the callback successfully. This callback receives the ID of the
    // newly-pinned widget (EXTRA_APPWIDGET_ID).
    PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,
            pinnedWidgetCallbackIntent);
    mAppWidgetManager.requestPinAppWidget(myProvider, null, successCallback);
}

上面,如果我试图通过成功的回调意图获取任何活动,即活动应该在固定快捷方式后立即打开,我没有得到它。谁能帮忙?

我也遇到过这个问题。您传入的意图应该是用于BroadcastReceiver的实现。

val callbackIntent = Intent(context, DemoWidgetPinnedReceiver::class.java)
val successCallback = PendingIntent.getBroadcast(
       context, 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT)
appWidgetManager.requestPinAppWidget(provider, null, successCallback)

成功添加小部件后,您将在广播接收器的onReceive方法中获得EXTRA_APPWIDGET_ID,然后您可以保存ID。固定小组件后,无需启动配置活动,因为可以将要为小组件保存的任何值传递到回调意图中。

我写了一篇关于此的博客文章,并创建了一个演示应用程序,希望这有帮助!https://medium.com/wearebase/android-oreo-widget-pinning-in-kotlin-398d529eab28https://github.com/sigute/WidgetsDemo

最新更新