2017-10-06 2 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に渡します(ユーザーがセットアップをキャンセルした場合または削除)私のカスタムデータにウィジェットIDを保存し、セットアップ後intent.setComponent(appWidgetInfo.configure);
  • を意図して使用し、
  • =ウィジェットを表示し、更新するために、IDを使用>私の場合のために、これはあります

質問...細かい作業

私は、次の使用できるように、私は、configureのフィールドで有効な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); 
0123!

これに代わる方法がありますか?

は私が何とか手動AppWidgetProviderInfoでウィジェットを作成し、mAppWidgetManager.getAppWidgetInfo(appWidgetId);との情報を再取得し、これは私がAppWidgetManager.ACTION_APPWIDGET_PICK意図を使用する場合に発生するようですと、私は満たされconfigureフィールドを持つことになりますしなければならないと思いますが、私は私を知りません手動でこれを行う...

答えて

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... 
    } 
} 
関連する問題