Create Widget on Application Installation

走远了吗. 提交于 2019-12-10 13:33:07

问题


I was just wondering, is there any way I can make an android widget show up on a user's home screen when they install my application? Also, can I let them choose to create a widget from within my application?


回答1:


I was just wondering, is there any way I can make an android widget show up on a user's home screen when they install my application?

No, sorry. None of your code gets executed upon install.

Also, can I let them choose to create a widget from within my application?

No, sorry. Only the home screen can add app widgets to the home screen.




回答2:


Where is no way to get callback upon installation. But in Android O you can pin your widget on first launch of your application.

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());
}

Also check out Google official documentation



来源:https://stackoverflow.com/questions/5101989/create-widget-on-application-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!