Currently I am studying the app widget with collection, and there are two points I am confused, see below codes
onUpdate codes
Intent serviceIntent=new Intent(context,StackService.class);
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.stack_widget);
remoteViews.setRemoteAdapter(R.id.stack_widget,serviceIntent);
remoteViews.setEmptyView(R.id.stack_widget,R.id.stack_empty);
Intent toastIntent = new Intent(context, StackWidget.class);
toastIntent.setAction(StackWidget.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.stack_widget, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i],remoteViews);
getViewAt codes
public RemoteViews getViewAt(int position) {
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.stack_item);
remoteViews.setImageViewResource(R.id.item_image,R.raw.logo);
remoteViews.setTextViewText(R.id.item_text,list.get(position));
Bundle extras = new Bundle();
extras.putInt(StackWidget.EXTRA_ITEM, position);
Intent fillIntent=new Intent();
fillIntent.putExtras(extras);
remoteViews.setOnClickFillInIntent(R.id.item_button,fillIntent);
return remoteViews;
}
First question is what purpose of below code,
serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)));
I read through google api doc, but can't find answer.
Second question is about toastIntent, why intent action is being set at onUpdate method rather than at getViewAt method? And why pending intent that has a custom action called TOAST_ACTION will be fired when button being pressed?
Thanks for answering my questions in advance.
Intent.toUri() can convert this Intent into a String holding a URI representation of it.
setDate often used in an undeclared Intent, which can start a component related to the date.
Related
I'm doing a widget with one stackView which i'm adding bitmaps(images) inside with a RemoteViewFactory.
My problem is when I want to do a onClickPendingIntent() for each image (that I have inside the stackView). I know I have to use setPendingIntentTemplate() instead, but I don't know how to go to an Activity when I press some widget's image.
Then this is what I have inside onUpdate() from widget class to create the Pending Intent Template:
Intent templateIntent = new Intent(Intent.ACTION_VIEW);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setPendingIntentTemplate(R.id.widget_stack_view,
templatePendingIntent);
And this is what I have in a RemoteViewsFactory class to handle the Pending Intent Template:
public RemoteViews getViewAt(int index){
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.grid_item_layout);
rv.setImageViewBitmap(R.id.image, images.get(index).getImage());
Intent fillInIntent = new Intent();
fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle());
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
return rv;
}
So far, this fillInIntent.putExtra(Intent.EXTRA_TEXT, images.get(index).getTitle()); is acting with the phone showing me a "use complete actions" where you can choose with which app you want to open, but if you see what i'm putting in the putExtra() is a String. But what I want is open a Activity and then pass with putExtra() a image (images.get(index).getImage()) that is a bitmap
I figured out what was wrong:
Intent templateIntent = new Intent(context, Activity_I_want_to_open.class);
templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent templatePendingIntent = PendingIntent.getActivity(
context, 0, templateIntent, 0);
Then, I didn't need to FLAG_UPDATE_CURRENT, because I want to updating nothing.
And in the RemoteViews side:
Intent fillInIntent = new Intent();
rv.setOnClickFillInIntent(R.id.image, fillInIntent);
I didn't need putExtra() because I'm not sending data to other activity.
How can i go to my App when i click on AppWidget
I saw no. of questions how to create widget in android home,but after craeting widget how to go to my app while clicking on that i didn't get from anyone of that.Please give me the guidelines to achieve this.
Thanks,
Since it is an AppWidget setting an onClickListener won't work. Instead you have to set an onClickPendingIntent on a specific view.
See the following code for the necessary steps (you should put this code into your onUpdate() method):
// Inflate the Widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.your_widget_layout);
// Create and set the Intent
Intent intent = new Intent(context, MyClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Add intent to a view (like a button)
remoteViews.setOnClickPendingIntent(R.id.your_button, pendingIntent);
Please note: The code only shows the relevant parts
As greenapps say :
Add a listener in your widged's view (e.g : click)
Create an intent and put extra information (e.g witch item is clicked)
Then start your activity.
e.g
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//...
Intent intent = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
//...
}
}
I need to show different data when the different widget will be clicked. For example widget one will show me activity with number 1 and widget to with numer 2 :)
Perhaps I can send some data by intent from widget, but then how to recive that data in activity class?
Widget class
Intent intent = new Intent(context, Information.class);
intent.putExtra("widget_id", appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget1x1);
remoteViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
Bundle extras = intent.getBundle();
String id = extras.getString("widget_id");
I am trying to detect when widget button is clicked but none of the Intent extras are showing up in the onReceive method.
onReceive gets called with every click but none of my Intent extras show up.
My code is below: I only hook up the toggle button in on update so not sure if this is correct. None of the extras show up and categories are null even though I set this.
onUpdate(Context context etc):
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
Intent buttonIntent = new Intent(context, MyWidgetProviderClass.class);
buttonIntent.setAction(ACTION_WIDGET_RECEIVER);
buttonIntent.putExtra("BUTTON_CLICKED", "buttonClick");
buttonIntent.putExtra("BUTTON",899);
PendingIntent muPendingIntent = PendingIntent.getBroadcast(context, 0,
buttonIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
buttonIntent.addCategory("buttonclick");
remoteViews.setOnClickPendingIntent(R.id.ToggleImageButton, myPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
onReceive():
intent.getIntExtra("BUTTON",-1); ---> 1
intent.getCategories() --- > null
Try FLAG_UPDATE_CURRENT instead of FLAG_CANCEL_CURRENT.
Also, your code may have a typo: you have muPendingIntent instead of myPendingIntent.
Also also, please do not use buttonclick as a category. Please namespace it (e.g., com.something.whatever.buttonclick), or remove it, as I am not sure why you would need it.
Here is a sample project demonstrating an app widget that, on a click, triggers an update on itself, with an extra (used to supply the app widget IDs).
Android Apparently Does not like re-use of the name ACTION_WIDGET_RECEIVER and removes those parameters. Created another ACTION just for toggle button, registered in the manifest and now the parameters show up.
I found that if the Intent that was used to create the Pending intent has any extras already in it then the new intent's extras are ignored. For example, if you follow the sample in the Android docs for building a Widget like so
Intent toastIntent = new Intent(context, StackWidgetProvider.class);
toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);
Then the line
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
Will prevent your new intent's extras from sticking. I removed that line and my new intent worked.
can anybody give example how to implement click event in appwidget in android ?
Thanks
You can use RemoteViews.setOnClickPendingIntent(R.id.view_id, intent) method for handle click event of app-widget. For more information you can refer below link
Click here
Use RemoteViews.setOnClickPendingIntent(R.id.view_id, intent) method.
The intent parameter should be a well formed Intent object which would match on of the filters at your manifest file.
Intent intent = new Intent(context, Activity.class);
Uri widgetId = Uri.parse("" + appWidgetId); // this line means
intent.setData(widgetId); // you can send a widget id
PendingIntent pintent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.clickButtoninWidget, pintent);
return views;