【发布时间】:2011-02-24 08:14:56
【问题描述】:
我只是想知道,当用户安装我的应用程序时,有什么方法可以让一个 android 小部件显示在用户的主屏幕上?另外,我可以让他们选择在我的应用程序中创建小部件吗?
【问题讨论】:
我只是想知道,当用户安装我的应用程序时,有什么方法可以让一个 android 小部件显示在用户的主屏幕上?另外,我可以让他们选择在我的应用程序中创建小部件吗?
【问题讨论】:
我只是想知道,当用户安装我的应用程序时,有什么方法可以让一个 android 小部件显示在用户的主屏幕上?
不,对不起。您的任何代码都不会在安装时执行。
另外,我可以让他们选择在我的应用程序中创建小部件吗?
不,对不起。只有主屏幕可以将应用小部件添加到主屏幕。
【讨论】:
安装时无法获得回调。但在 Android O 中,您可以在应用程序首次启动时固定您的小部件。
AppWidgetManager mAppWidgetManager =
context.getSystemService(AppWidgetManager.class);
AppWidgetProviderInfo myWidgetProviderInfo = new AppWidgetProviderInfo();
ComponentName myProvider = myWidgetProviderInfo.provider;
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.getIntentSender());
}
另请查看谷歌官方documentation
【讨论】: