我正在尝试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);
}在上面,如果我试图通过成功回调意图获取任何活动,即该活动应该在锁定快捷方式后立即打开,则我无法获得它。有人能帮上忙吗?
发布于 2017-11-17 21:55:12
我也遇到过这种情况。您传入的意图应该是针对您的BroadcastReceiver实现的。
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
https://stackoverflow.com/questions/47158435
复制相似问题