Jong
Jong

Reputation: 9115

Android - AppWidget's pending intent does not fire

I have an app widget with a Button in it's layout. When clicking the button, an intent is fired which calls my broadcast receiver.

It works just fine, but occasionally, after using the "Clear memory" button in the Task Manager, the widget gets stuck - clicking on it does nothing. But it can still receive updates from my app, if its running.

I'm not sure if the fact that the pending intent isn't fired is the memory clearing fault, or my fault. Anyway, here's the code:

Registering the pending intent (onUpdate method of the app widget)

    Intent intent = new Intent(context, ServiceControl.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
    views.setOnClickPendingIntent(R.id.appwidgetbutton, pendingIntent);

and then updating the widgets with the views.

Here is the decleration of the app widget provider:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:initialLayout="@layout/appwidget"
android:updatePeriodMillis="0">
</appwidget-provider>

I don't want the system to call widget updates, I only update it from my app itself.

So why does the pending intent stop firing?

Thanks in advance.

Upvotes: 4

Views: 1511

Answers (1)

Radu
Radu

Reputation: 2074

@Jong

@CommonsWare

Hi guys, I figured it out. Ofc this is an Android issue, the receiver should ALWAYS receive. Now how to get around it? Obviously all the widgets are working, so there must have been a simple out there.

I read on SO somewhere (trying to find the guy) reminding us all that the widget class is actually extending a BroadcastReceiver.

So, you could register the widget (in the manifest) to receive the threats itself. Thus the entire system is self-contained in the class instance of AppWidgetProvider.

Now, for communicating back with the app, you can in the onReceive call any static class of your app, and LocalBroadcastManager won't fail you if the app is active. If it's not active, your buttons should be starting activities anyway!

Should you want the code, I can detail it.

Upvotes: 1

Related Questions