2017-10-06 16 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 

정상 접근

  • AppWidgetManager.ACTION_APPWIDGET_PICK을 사용하여 다음 위젯 ID를 생성하고 (그것이 작동하는 방법 일반적으로 내 사용 사례에 대한 중요한,하지만이 설명합니다) 사용자가 위젯을 선택할 수있게하려는 의도
  • 을 선택한 다음 데이터를 AppWidgetManager.ACTION_APPWIDGET_CONFIGURE으로 전달합니다. 의도와 나의 경우를 들어 위젯
  • =>를 위젯 ID를 저장, 설치 후 intent.setComponent(appWidgetInfo.configure);
  • 를 사용 (또는 사용자가 설치를 취소시 삭제) 내 사용자 데이터 및 표시하기 위해 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); 
0123!

또는 대체 방법이 있습니까?

나는 어떻게 든 수동으로 AppWidgetProviderInfo와 위젯을 만든 다음 mAppWidgetManager.getAppWidgetInfo(appWidgetId);와 정보를 reget이 내가 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... 
    } 
}