簡體   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