繁体   English   中英

React-native将小部件添加到android主屏幕

[英]React-native add a widget to the android home screen

我有以下代码: https : //github.com/lucas-kejan/React-Widget/blob/master/android/app/src/main/java/com/androidwidgetpoc/BackgroundTaskBridge.java#L26

当该方法被调用时,应将小部件添加到android主屏幕,但不起作用。

它给了我以下错误:

lenght = 0; index = 0.
BackgroundTaskBridge.java:31

好吧,让我们看一下您想做的事情-让用户选择小部件并在启动器中占据一席之地。 如果要使其成为Oreo设备之前的设备,则没有api用于后续操作。 因此,要运行PICK_APPWIDGET意图,您需要appWidgetId。 自己获取appWidgetId的唯一方法是将id绑定到appWidgetInfo,但这件事需要BIND_APPWIDGET权限,该权限仅对系统可用。 因此,在Oreo之前的设备上,不可能做出自己想要的东西(当然,如果您不是系统应用程序的话)。

在Oreo设备上,我们有一个新的固定小部件API。

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

不要忘记它需要api版本26+。 您可以在此处查看以下api的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM