App widget button to trigger onUpdate - android

public class ExampleAppWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
Intent intent = new Intent(context, Fragment_testActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app
// widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Why doesn't the button1 trigger onUpdate (nor onReceive) when the widget is added to the homescreen and button1 is clicked? Adding/removing the widget triggers those events, but not clicking the button.
Manifest, widget declaration, widget layout declaration

First thing I see:
new Intent(context, Fragment_testActivity.class);
You specify an activity as the receiving component. The activity has nothing to do with the update process. Try to set ExampleAppWidgetProvider as the component of the intent or don't specify a component at all (e.g. by using new Intent()).

Related

How to add activity button in Android widget

This is the first time that I need to create an home widget for android.
I need to define buttons in my widget_layout to launch some activities so I have created a class WidgetProvider that extends AppWidgetProvider and shows the Widget in the Home, but I have noticed that there isn't onCreate, so I haven't understod how to exactly link the button in my layout xml with a listener that can open the desired activity.
There are many widget on the market that have buttons of this kind.
how should I do?
In widgets you have to use RemoteViews instead of View, and there is no findViewById(), and therefore no setOnClickListener(). What you can do however is use remoteViews.setOnClickPendingIntent(id, pendingIntent). Here's a minimal way to open the activity using this:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent appIntent = new Intent(context, MyActivity.class);
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE, appIntent, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button, appPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

Service enabling itself when it shouldn't

I've added a widget for my application, basically clicking on the widget enables a service.
But sometimes i experience random enabling of the service even if i didn't press the widget
I followed the android developer guide to make the widget and the code looks like this
public class ToggleWidget extends AppWidgetProvider {
static RemoteViews remoteViews;
boolean status = false;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
remoteViews = new RemoteViews(context.getPackageName(), R.layout.toggle_widget);
Intent intent = new Intent(context.getApplicationContext(), WakeService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.bulb_widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
context.startService(intent);
Log.w(getClass().getName(), "Widget Worked");
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
This code should run the service intent when i click the ImageButton.
Clicking on it works as it should, but i get those random enablings (the service displays a notification when it's enabled, so i know it's running)
Can you spot something wrong in the code?
Take out
context.startService(intent);
Since the PendingIntent already starts the Service when the widget is clicked, having an explicit context.startService() in onUpdate() will make the Service start regardless of whether or not the widget is clicked.

Why does my Android 2.3 widget not respond to a click after booting?

I have an app with a widget. I want to start the app with the widget but after booting my widget does not respond to a click. Only after starting the app normally (and closing it again) does the widget start to respond to the click on the widget itself.
This is how I setup the widget with the AppWidgetProvider:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{ final int ids = appWidgetIds.length;
for(int i=0;i<ids;i++)
{ int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, TestReceiver.class);
intent.setAction(ACTION_WIDGET_RECEIVER);
intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget_root, pi);
views.setTextViewText(R.id.widgetclock, "Loading...");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
What do I have to change so that my app registers clicks on the widget after a boot without having to start it first at least once ?
Try creating a BroadcastReceiver that responds to the BOOT broadcast and resets the widget's PendingIntent: I don't believe widget PendingIntents survive device reboots.

Android Widget onClick

I want to be able to click on a widget and launch a dialog box. I have read the official documentation as some of the unofficial ones. I initially wanted to launch a new activity but even this fails. I get the following in Logcat but I cant really see anything.
11-14 21:28:47.929: INFO/ActivityManager(116): Starting: Intent { flg=0x10000000 cmp=com.android.app/.Execute bnds=[179,89][300,160] } from pid -1
I guess the above means that the intent was passed... But the activity was actually not started. Should the activity to be started be a normal one?
The code used is:
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Any thoughts?
The most likely cause of the problem would be that you haven't declared ExampleActivity in your Manifest.
<UPDATE>
You could also try using a unique number for argument 2 of your PendingIntent creation and also put a sensible flag in argument 4:
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT );

Android widget button unclickable when adding more than one widget

I have a widget which is basically a big button (with some images in the background). The button works fine if there's just one widget on the home screen (or more than one after a phone reboot), but if I try to add another widget, the button suddenly stops reacting (on the second widget).
Been struggling with this for months now. Hopefully you'll be able to assist.
public class StatsWidget extends AppWidgetProvider {
public static String ACTIONWIDGETCLICK = "MyWidgetClick";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent intentt = new Intent(context, StatsService.class);
context.startService(intentt);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intent = new Intent(context, StatsWidgetActivity.class);
intent.setAction(ACTIONWIDGETCLICK);
intent.putExtra("widgetId", appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
The only error I see in your code is you forgot to add
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This must be used when starting an Activity from a service/broadcastreciever/etc. Otherwise are you updating the widget from anywhere else in your app? I recently had a problem with the pending intent unresistering itself because I updated the widget from within my app and didn't add the
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
to the update code.
Hope this helps

Categories

Resources