We can not use onClickListenerdirectly with the view in widgets. Instead, we should wrap ours Intentwith PendingIntentand setOnClickPendingIntentto view.
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i : appWidgetIds) {
startBrowsing(context, appWidgetManager, i);
}
}
private void startBrowsing(Context ctx,
AppWidgetManager appWidgetManager, int widgetID) {
RemoteViews widgetView = new RemoteViews(ctx.getPackageName(), R.layout.widget);
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
PendingIntent pIntent = PendingIntent.getActivity(ctx, widgetID, intent, 0);
widgetView.setOnClickPendingIntent(R.id.viewID, pIntent);
appWidgetManager.updateAppWidget(widgetID, widgetView);
}
Set the update period in the metadata.xml file to 0, because we update it manually
android:updatePeriodMillis="0"
And don't forget
<uses-permission android:name="android.permission.INTERNET"/>
source
share