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.
Related
I am trying to do widget, which will update itself on click.
Co, I created following code, which works like a charm - but only for about 20minutes then it stopped working (it does not update on click anymore, but classic widget self-update after specified period in XML works)
It starts working again if I add another instance of widget, then all intents are registered again.
Have anyone else faced the problem? Can you see something wrong with my code?
In my widget Provider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
Log.d(tag, "onUpdate");
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_exchange_rate_small);
Intent intent = new Intent(context, ExchangeRateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Register onClick action for pendingIntent
views.setOnClickPendingIntent(R.id.wdExchangeRate, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
try {
// Perform first update
pendingIntent.send();
} catch (CanceledException e) {
Log.e(tag, "First update has been canceled, this should not normally happend!");
e.printStackTrace();
}
}
How my service is declared:
public class ExchangeRateWidgetService extends IntentService {
protected void onHandleIntent(Intent intent) {
// Doing something ...
}
}
I think I solved it, the problem was in IntentService, service updated widget via remote view, but does not registered setOnClickPendingIntent.
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.
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()).
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 );
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