首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android O中,如何在成功回调意图的同时以编程方式锁定小部件快捷方式?

在Android O中,如何在成功回调意图的同时以编程方式锁定小部件快捷方式?
EN

Stack Overflow用户
提问于 2017-11-07 20:48:00
回答 1查看 1.1K关注 0票数 0

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

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

给出的示例代码是:

代码语言:javascript
复制
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);
}

在上面,如果我试图通过成功回调意图获取任何活动,即该活动应该在锁定快捷方式后立即打开,则我无法获得它。有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-17 21:55:12

我也遇到过这种情况。您传入的意图应该是针对您的BroadcastReceiver实现的。

代码语言:javascript
复制
val callbackIntent = Intent(context, DemoWidgetPinnedReceiver::class.java)
val successCallback = PendingIntent.getBroadcast(
       context, 0, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT)
appWidgetManager.requestPinAppWidget(provider, null, successCallback)

成功添加widget后,您将在BroadcastReceiver的onReceive方法中获取EXTRA_APPWIDGET_ID,然后可以保存ID。widget固定后不需要启动配置活动,因为您可以将想要为widget保存的任何值传递到回调意图中。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47158435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档