Calling an activity on HomeScreenWidget click
I am trying to open my activity when a user clicks on a button in my HomeScreen Widget. But I guess the app is not responding to my onclick. Here is my code snippet :
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.layout_appwidget);
Intent launchActivity = new Intent();
launchActivity.setClass(context,UpdatesScreen.class);
launchActivity.setFlags(Intent.FLAG_ACT开发者_如何学PythonIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0, launchActivity, 0);
remoteViews.setOnClickPendingIntent(R.id.iv_widget, pendingIntent);
ComponentName thisWidget = new ComponentName(context, MyAppWidgetProvider.class.getName());
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);
I have copied the same code in onUpdate as well as onEnabled method of MyAppWidgetProvider class. But still it's not working at all.
I have read many post regarding this issue but all are using the same code. Am I doing something wrong?? What is the right way to do the same.
Thanks in advance.
I have found the mistake. Posting my answer to help someone facing the same issue.
Actually, I was overriding both the method onUpdate()
as well as onReceive()
. If we override onReceive()
, in that case onUpdate()
never gets called. and I wasn't using ACTION_APPWIDGET_UPDATE
as an action in onReceive()
too.
精彩评论