Reputation: 9419
I need a way to determine which AppWidgetProvider is active for the current widget in the configure Activity. My current solution results in a NullException on some devices, the appWidgetInfo.provider is null somehow.
Is there a better way to determine which AppWidgetProvider is corresponding to a specific appWidgetId.
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(widgetId);
Intent intent = new Intent();
intent.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
widgetId);
intent.setAction(RemoteWidgetViewsFactory.KEY_WIDGET_SETTING_REFRESH);
String appWidgetProviderName = appWidgetInfo.provider.getClassName();
if(appWidgetProviderName.equals(WidgetProvider.class.getName())) {
intent.setClass(context, WidgetProvider.class);
}
else if(appWidgetProviderName.equals(WidgetProviderLarge.class.getName())) {
intent.setClass(context, WidgetProviderLarge.class);
}
sendBroadcast(intent);
Upvotes: 0
Views: 851
Reputation: 818
I actually had the same problem cause I also needed to determine the actual AppWidgetProvider class. Your question did help me and I probably can help you. Just implement a method like this and you dont have to write code for each AppProvider class:
private static Class<?> getAppWidgetProviderClass(Context context, int appWidgetId){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
String appWidgetProviderName = appWidgetInfo.provider.getClassName();
Class<?> myProvider = null;
try {
myProvider = Class.forName(appWidgetProviderName);
} catch (ClassNotFoundException cnf){
Log.e("myProviderClass","Class:" + appWidgetProviderName + " not found!");
}
return myProvider;
}
after use it for the class definition Intent:
Intent myIntent = new Intent(context, getAppWidgetProviderClass(context, appWidgetId));
Hope it helps you!
Upvotes: 1