2017-10-06 84 views
1

而不是向用戶顯示窗口小部件提供程序列表,我讓他選擇一個然後配置窗口小部件,我手動加載所有窗口小部件提供程序並讓用戶將這些窗口小部件拖放到我的應用程序中。 只要用戶放棄這樣一個圖標,我想要創建和配置這個小部件。我該怎麼做呢?使用已知的AppWidgetProviderInfo創建窗口小部件

代碼註釋

// 1) get a list of all app widget providers 
List<AppWidgetProviderInfo> providers = mAppWidgetManager.getInstalledProviders(); 

// 2) Display the list to the user and let him select a widget provider => done via custom UI 

// 3) handle the user selected widget and create it 
// 3.1) create new widget id 
int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); 
// 3.2) configure widget 
// ??? How do I do this now? appWidgetInfo.configure = null, so I can't use this as I normally would do it 
// 4) Save the appWidgetId or delete it again depending on if the user finished the setup or cancelled it 

普通方法(因爲我的用例並不重要,但總體上描述了它是如何工作)

  • 創建窗口小部件的ID,然後使用AppWidgetManager.ACTION_APPWIDGET_PICK意圖讓用戶在選擇後選擇一個小部件
  • ,將數據傳遞給AppWidgetManager.ACTION_APPWIDGET_CONFIGURE意圖和使用intent.setComponent(appWidgetInfo.configure);
  • 設置後,保存的小工具ID(或刪除,如果用戶取消設置)在我的自定義數據和使用id來顯示和更新部件
  • =>對於我而言,這是做工精細...

問題

如何獲得有效的AppWidgetProviderInfo與組態現場= NULL,這樣我可以使用以下:

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 
// appWidgetInfo.configure is null for appWidgetInfo received from mAppWidgetManager.getInstalledProviders() 
// it is not if the appWidgetInfo is received via the AppWidgetManager.ACTION_APPWIDGET_PICK intent... 
intent.setComponent(appWidgetInfo.configure); 
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
activity.startActivityForResult(intent, WIDGET_CONFIGURE_REQUEST_CODE); 

或者還有其他方法嗎?

我覺得我莫名其妙必須手動創建與AppWidgetProviderInfo一個小部件,然後用reget的mAppWidgetManager.getAppWidgetInfo(appWidgetId);信息,然後我將有一個充滿configure場,因爲這似乎是,如果我使用AppWidgetManager.ACTION_APPWIDGET_PICK意圖發生,但我不知道我這樣做手動...

回答

0

我可以解決這個問題。 ACTION_APPWIDGET_PICK意圖將小部件綁定到id,因此必須在我的情況下手動完成:

private static void configureWidgetManually(AppCompatActivity activity, AppWidgetProviderInfo appWidgetInfo) { 
    int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); 

    boolean hasPermission = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetInfo.provider); 
    if (!hasPermission) { 
     // this if part is untested, I never get into this part, but from my understanding it should work like this... 
     Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, appWidgetInfo.getProfile()); 
     } 
     // Result of this can be handled the same way as the result of AppWidgetManager.ACTION_APPWIDGET_PICK, so we can even use the same request code... 
     activity.startActivityForResult(intent, WIDGET_SELECTOR_REQUEST_CODE); 
    } else { 
     // Widget is bound, we can continue as if we would have used the `AppWidgetManager.ACTION_APPWIDGET_PICK` intent 
     appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); 
     configureWidget(activity, appWidgetId, appWidgetInfo); 
    } 
} 

private static void configureWidget(AppCompatActivity activity, int appWidgetId, AppWidgetProviderInfo appWidgetInfo) { 

    if (appWidgetInfo.configure != null) { 
     Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 
     intent.setComponent(appWidgetInfo.configure); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
     activity.startActivityForResult(intent, WIDGET_CONFIGURE_REQUEST_CODE); 
    } else { 
     // widget is configured, do whatever you want with the id... 
    } 
} 
相關問題