I try to make a configuration Activity that updates a widget. I also want to make the widget clickable. I managed to update the widget but it won't let me click on it (nothing happend).
This is my code.
private void generate() {
// Create an Intent to launch WidgetConfigurationActivity screen
Intent intent = new Intent(context, ConfigureWidgetActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
// This is needed to make this intent different from its previous intents
intent.setData(Uri.parse("tel:/" + (int) System.currentTimeMillis()));
// Creating a pending intent, which will be invoked when the user
// clicks on the widget
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Getting an instance of WidgetManager
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
// Instantiating the class RemoteViews with widget_layout
RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_layout);
// Attach an on-click listener to the widget
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
// Tell the AppWidgetManager to perform an update on the app widget
appWidgetManager.updateAppWidget(mAppWidgetId, views);
//-----BROADCAST
Intent refreshIntent = new Intent(this, WidgetProvider.class);
refreshIntent.putExtra("parser", parser);
sendBroadcast(refreshIntent);
//-----
// Return RESULT_OK from this activity
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
Now, if I put this piece of code in comment
Intent refreshIntent = new Intent(this, WidgetProvider.class);
refreshIntent.putExtra("parser", parser);
sendBroadcast(refreshIntent);
The widget is of course not updated but is clickable.
When I do
//----FIRST NOW
Intent refreshIntent = new Intent(this, WidgetProvider.class);
refreshIntent.putExtra("parser", parser);
sendBroadcast(refreshIntent);
//----THIS COME AFTER
// Attach an on-click listener to the widget
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
// Tell the AppWidgetManager to perform an update on the app widget
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Here is my AppWidgetProvider (in case)
public class WidgetProvider extends AppWidgetProvider {
/** https://stackoverflow.com/questions/17434898/sending-data-to-my-homescreen-widget */
@Override
public void onReceive(Context context, Intent intent) {
Parser parser = (Parser) intent.getSerializableExtra("parser");
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName name = new ComponentName(context, WidgetProvider.class);
int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
final int N = appWidgetId.length;
if (N < 1)
{
return ;
}
else {
int id = appWidgetId[N-1];
updateWidget(context, appWidgetManager, id , parser);
}
}
static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId,
Parser parser){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
if (parser != null) {
views.setTextViewText(R.id.regio, parser.getRegio());
views.setTextViewText(R.id.max, parser.getMax());
views.setTextViewText(R.id.min, parser.getMin());
views.setTextViewText(R.id.windf, parser.getWindf());
views.setImageViewResource(R.id.wind, parser.getWind());
views.setImageViewResource(R.id.first, parser.getFirst());
views.setImageViewResource(R.id.second, parser.getSecond());
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
I think it's something stupid :)
Found something (same code as mine?) - How to update a widget on app start